0.0.3
, set config var without os.environ
.Flask-DotEnv will directly set (add, update and map as alias) variable from .env file.
$ pip install Flask-DotEnv
Basic usage.
from flask import Flask from flask.ext.dotenv import DotEnv app = Flask(__name__) env = DotEnv(app)
As factory pattern.
env = DotEnv() env.init_app(app)
class Config: SECRET_KEY = ":'(" ... @classmethod def init_app(self, app) env = DotEnv() env.init_app(app) # alias is optional # this will set var like a `DEVELOPMENT_DATABASE_URL` as `SQLALCHEMY_DATABASE_URI` prefix = self.__name__.replace('Config', '').upper() env.alias(maps={ prefix + '_DATABASE_URL': 'SQLALCHEMY_DATABASE_URI' })
You can pass .env file path as second argument of init_app()
.
env.init_app(app, env_file="/path/to/.env", verbose_mode=True)
env_file
) is optional. default is os.path.join(os.getcwd(), '.env')
.verbose_mode
) is also optional. default False
.verbose_mode
is True, then server outputs which vars will be set.* Overwriting an existing config var: SECRET_KEY * Setting an entirely new config var: DEVELOPMENT_DATABASE_URL * Mapping a specified var as a alias: DEVELOPMENT_DATABASE_URL => SQLALCHEMY_DATABASE_URI ...
alias() method takes a dict argment.
env.alias(maps={ 'TEST_DATABASE_URL': 'SQLALCHEMY_DATABASE_URI', 'TEST_HOST': 'HOST' })
Run unittest.
$ python tests/tests.py
Inspired from:
See another packages, its also set config vars:
BSD 2-Clause License