Reputation: 257
How to use app.config.from_envvar()?
I have look at Flask doc and search for this topic what I all knows is to do this.
DATABASE = 'flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
app = Flask(__name__)
app.config.from_envvar(’FLASKR_SETTINGS’, silent=True)
Will this load the config from FLASKR_SETTINGS ? and how can the program knows what is FLASKR_SETTINGS? should I also set something like this (the path to the config file)?:
FLASKR_SETTINGS = desktop/my_flask_project/FlaskConfig
and move the first 3 lines into that file and when I run this file, it will be loaded in?
and I only choose to use of of these right? between the app.config.from_envvar (this one for load config from external file)or the app.config.from_object(name)(this one will load config within the file) ? Am i understand correctly?
Upvotes: 16
Views: 17043
Reputation: 159915
envvar
is short for Environment Variable
. If you are using a Linux-based OS (Ubuntu, Mac, etc.) then when you run a normal shell you are probably running bash
. To set an environment variable in bash you simply do:
$ SOME_NAME=some_value
So in the case of a Flask application that configured itself from the FLASKR_SETTINGS
environment variable, you would do:
$ FLASKR_SETTINGS=/path/to/settings/file.ext
$ python your_script.py
What Flask does is simply import that file as if it was an ordinary Python file and pull out every UPPERCASE_ONLY name in the file (Any other caseCombination will be ignored).
The same is true for from_object
- in fact, from_object
can also take an importable string:
app.config.from_object("importable.configuration")
Finally, note that you do not have to only have one config call - multiple calls can be used:
app.config.from_object("your.package.default.config")
app.config.from_envvar("YOUR_APPS_ENVVAR", silent=True)
Upvotes: 11
Reputation: 76792
This loads Flask configuration from a file indicated by an environment variable.
This is covered in the documentation here: http://flask.pocoo.org/docs/config/#configuring-from-files
$ export YOURAPPLICATION_SETTINGS=/path/to/settings.cfg
$ python run-app.py
* Running on http://127.0.0.1:5000/
* Restarting with reloader...
http://en.wikipedia.org/wiki/Environment_variable
Upvotes: 9