fromenvboximportget_environment# Let's detect current environment type and get its object.# * See and use `get_environment` function arguments to impose restrictions upon detection system.## Default detection sources:# 1. ``PYTHON_ENV`` env variable# 2. ``environment`` file contents## By default this function will also try to read env variables from .env files.env=get_environment()env.name# >> developmentenv.is_production# >> Falseenv.get('HOME')# The same as env['HOME'] and env.HOME# >> /home/idle/env.getmany('PYTHON')# {'UNBUFFERED': '1', 'IOENCODING': 'UTF-8', 'PATH': ...}# We can also try to cast env values into Python natives.env.getmany_casted('PYTHON')# Note that `UNBUFFERED` is int now.# {'UNBUFFERED': 1, 'IOENCODING': 'UTF-8', 'PATH': ...}
MY_VAR_1=value1
HOME=/home/other/
# comments are ignored, just as lines without definitions
# mathing quotes (" and ') are stripped
MY_QUOTED="some quoted "
# ${VARNAME} will be replaced by value from env (if available)
MY_VAR_2="${MY_QUOTED}"
# multiline with dangling quotes
MULTI_1="
line1
line2
"
# multiline classic
MULTI_2="line1
line2
line3"
# multiline as one line
MULTI_3="one\ntwo"
Note
envbox will try to load such files from the current working directory for the current environment type automatically.
# Somewhere in your setting module declare settings:class_Settings(SettingsBase):ONE=1SOME='two'ANOTHER=TrueSettings=_Settings()# Now access those settings from other modules(s).ifSettings.ANOTHER:Settings.SOME='three'
Accessing any setting which was not set in the session, will lead to appropriate environment variable probing.
fromenvboximportget_environment,PRODUCTIONfromenvbox.envsimportregister_type# Let's make `prod` string identify production environment.register_type(PRODUCTION,alias='prod')# Now if someone has used `prod`# we correctly identify it as production environment.get_environment().is_production# True
envbox features import_by_environment() function which automatically imports symbols of a submodule
of a package for the given (or detected) environment into globals of an entry-point submodule.
Note
This could be useful not only for Django-projects where submodule-based settings definition is rather usual but also for various other cases.