16

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?

Vishal Patel
  • 1,715
  • 16
  • 26
Team
  • 247
  • 1
  • 2
  • 11

2 Answers2

11

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)
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • So in my case if I only use the config from outside I only need app.config.from_envvar() and do not have to use app.config.from_object() right? you just tell me multiple calls is optional? the file that contain config code have to be .py? or .ext? I see in the Doc it says .cfg o_O !!! – Team Nov 19 '13 at 04:59
  • 2
    @Team - correct, you can use any combination of `from_object` and `from_envvar` that you want. As to the extension, it can be anything you want at all (`.py`, `.cfg`, `.i-just-made-this-up-right-now` ... they will all work). – Sean Vieira Nov 19 '13 at 05:17
  • I typed . venv/bin/activate then export FLASKR_CONFIG=Users/Team/desktop/flask\ project/flaskr/config.py and then python flaskr.py then it has error SyntaxError: Non-ASCII character '\xe2' in file flaskr.py on line 8, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details (venv)Tammarats-MacBook-Air:flaskr Team$ <<---- the line 8 is this code app.config.from_envvar(’FLASKR_CONFIG’, silent=True) I did something wrong ? what the error says about? – Team Nov 19 '13 at 05:32
  • I found it, it is about using '' instead of "" so why they put '' on the website OMG - -" thank you so much Mr. Sean Vieira you r rock – Team Nov 19 '13 at 05:46
  • But I wonder can't we use app.config.from_envvar('YOUR_APPS_ENVVAR', silent=True) instead of app.config.from_envvar("YOUR_APPS_ENVVAR", silent=True) ??? so why it is used in the doc by Flask ??? – Team Nov 19 '13 at 05:55
  • alright I found something weird, If i copy the single quote 'something' from the PDF doc from Flask it will not work as single quote 'something' in the coder, so I have to replace it again with the single quote 'something' from my own keyboard. This is to tell all the beginner. Replace single quote by your self with the code that you copy from the PDF from Flask!!!!!!! – Team Nov 19 '13 at 06:10
  • 1
    @Team - looks like the PDF has proper curly quote marks, rather than the single quote apostrophe you need to use for Python strings. As you found out, replacing it with an apostrophe or a double-quote fixes the issue :-) – Sean Vieira Nov 19 '13 at 06:59
  • The relevant documentation http://flask.pocoo.org/docs/0.12/config/#configuring-from-files – Michael Grazebrook Jul 25 '17 at 13:09
9

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

FogleBird
  • 74,300
  • 25
  • 125
  • 131