.env file to flask style config class for applications.0.0.3 and above support setting config variables without using os.environ.Flask-DotEnv will directly set (add, update, map as alias and eval as
literal) variables from .env file, and cast them to Python native types
as appropriate.
(optional)
alias()makes alias varseval()evaluate var to literal (viaast)
- https://gitlab.com/grauwoelfchen/flask-dotenv.git (main)
- https://github.com/grauwoelfchen/flask-dotenv.git
$ pip install Flask-DotEnv
from flask import Flask from flask_dotenv import DotEnv app = Flask(__name__) env = DotEnv(app)
As a factory pattern.
env = DotEnv() env.init_app(app)
env module may be useful in your Config class.class Config:
SECRET_KEY = ":'("
...
@classmethod
def init_app(self, app)
env = DotEnv()
env.init_app(app)
Then in your app:
from config import Config app = Flask(__name__) app.config.from_object(config[config_name])
See also:
flask.Config.from_object (Flask's API documentation)
You can pass the .env file path as a second argument of init_app().
env.init_app(app, env_file="/path/to/.env", verbose_mode=True)
env_file) is optional, and the default is os.path.join(os.getcwd(), '.env').verbose_mode) is also optional, and defaults to False.verbose_mode is True, then server outputs nice log message showing which vars will be set,* Overwriting an existing config var: SECRET_KEY * Setting an entirely new config var: DEVELOPMENT_DATABASE_URL * Casting a denoted var as a literal: MAIL_PORT => <class 'int'> * Making a specified var as an alias: DEVELOPMENT_DATABASE_URL -> SQLALCHEMY_DATABASE_URI ...
The alias() method takes a dict argument. Each key is the existing config var,
while each value is the new alias.
env.alias(maps={
'TEST_DATABASE_URL': 'SQLALCHEMY_DATABASE_URI',
'TEST_HOST': 'HOST'
})
Here's an example of its use:
class Config:
SECRET_KEY = ":'("
...
@classmethod
def init_app(self, app)
env = DotEnv()
env.init_app(app)
# The following will store in `SQLALCHEMY_DATABASE_URI` the value
# in, for example, `DEVELOPMENT_DATABASE_URL`
prefix = self.__name__.replace('Config', '').upper()
env.alias(maps={
prefix + '_DATABASE_URL': 'SQLALCHEMY_DATABASE_URI'
})
class DevelopmentConfig(Config):
DEBUG = True
SQLALCHEMY_DATABASE_URI = None
config = {
'development': DevelopmentConfig
}
eval() also takes a dict argument. These keys are also the existing config
var, while the values are the type they should evaluate to. If the type is
something else, the config var is skipped with a log message shown.
env.eval(keys={
'MAIL_PORT': int,
'SETTINGS': dict
})
And here's an example of its use:
class Config:
SECRET_KEY = ":'("
...
@classmethod
def init_app(self, app)
env = DotEnv()
env.init_app(app)
# `MAIL_PORT` will be set the the integer verson of the value found there
# using `ast.literal_eval`.
env.eval(keys={
MAIL_PORT: int
})
The following lines are all valid.
SECRET_KEY="123"
USERNAME=john
DATABASE_URL='postgresql://user:password@localhost/production?sslmode=require'
FEATURES={'DotEnv': True}
# comment and blank lines are also supported
export ENV="production"
export env="staging"
Run the unit tests with:
$ python setup.py test
Inspired by:
Other packages that also set configuration variables:
BSD 2-Clause License