Skip to content
Myr

Add basic how-to on using envvars for config

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by tristan fisher · committed by tristan fisher · parent 9d82993 · Browse files · 1 file+48 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+48 added−0 removed

  • other:+48 added−0 removed

1 file+48 added−0 removed

modified:docs/config.rst+48 added
373 unmodified lines
374methods on the config object as well to load from individual files. For a375complete reference, read the :class:`~flask.Config` object's376documentation.377378Configuring from Environmental Variables379----------------------------------------380381In addition to pointing to configuration files using environment variables, you382may find it useful (or necessary) to control your configuration values383directly from the environment.384385Environment variables can be set on Linux or OS X with the export command in the386shell before starting the server::387388 $ export SECRET_KEY='?\xbf,\xb4\x8d\xa3"<\x9c\xb0@\x0f5\xab,w\xee\x8d$0\x13\x8b83'389 $ export DEBUG=False390 $ python run-app.py391 * Running on http://127.0.0.1:5000/392 * Restarting with reloader...393394On Windows systems use the `set` builtin instead::395396 >set SECRET_KEY='?\xbf,\xb4\x8d\xa3"<\x9c\xb0@\x0f5\xab,w\xee\x8d$0\x13\x8b83'397 >set DEBUG=False398399While this approach is straightforward to use, it is important to remember that400environment variables are strings -- they are not automatically deserialized into Python401types.402403Here is an example of a configuration file that uses environmental variables::404405 # Example configuration406 import os407408 ENVIRONMENT_DEBUG = os.environ.get("DEBUG", default=False)409 if ENVIRONMENT_DEBUG.lower() in ("f", "false"):410 ENVIRONMENT_DEBUG = False411412 DEBUG = ENVIRONMENT_DEBUG413 SECRET_KEY = os.environ.get("SECRET_KEY", default=None)414 if not SECRET_KEY:415 raise ValueError("No secret key set for Flask application")416417418Notice that any value besides a empty string will be interpreted as a boolean True value in Python, which419requires care if an environment may explicit setting values intended to be False.420421Make sure to load the configuration very early on, so that extensions have422the ability to access the configuration when starting up. There are other423methods on the config object as well to load from individual files. For a424complete reference, read the :class:`~flask.Config` object's425documentation.426427Configuration Best Practices428----------------------------429430The downside with the approach mentioned earlier is that it makes testing
167 unmodified lines