Skip to content
Myr

Merge pull request #2366 from tristanfisher/master

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

Add basic how-to on using envvars for config

authored by David Lord · committed by GitHub · parents 9d82993, 01d2f72 · 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 Environment Variables379--------------------------------------380381In addition to pointing to configuration files using environment variables, you382may find it useful (or necessary) to control your configuration values directly383from the environment.384385Environment variables can be set on Linux or OS X with the export command in386the shell 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 deserialized401into Python types.402403Here is an example of a configuration file that uses environment 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 an empty string will be interpreted as a boolean419``True`` value in Python, which requires care if an environment explicitly sets420values intended to be ``False``.421422Make sure to load the configuration very early on, so that extensions have the423ability to access the configuration when starting up. There are other methods424on the config object as well to load from individual files. For a complete425reference, read the :class:`~flask.Config` class documentation.426427Configuration Best Practices428----------------------------429430The downside with the approach mentioned earlier is that it makes testing
167 unmodified lines