Earlier this month I released python-envvars, a python library which can read the contents of a .env file adding the key->values into pythons
os.environ dictionary. The idea behind envvars is to make it easier to add environment variables into a python web application (django, flask etc).
By storing configuration settings within environment variables it allows your application to be easily moved from server to server without having to alter the code base of your application. Using
.env files as to the host systems environment variables means you simply have to upload the
.env file not messing around with the host system setup. The
.env files can be stored in you secure files location keeping credentials safe.
.env file looks something like:
SECRET_KEY=AbCdEfG DEBUG=False SOME_VARIABLE=1348904
The envvars package is available on pypi and can be installed using:
pip install envvars
To load the contents of the
.env file into your application you simply call.
import envvars import os envvars.load('/path/to/file/.env') print os.environ['SECRET_KEY'] > AbCdEfG
Envvars also has a
get method which will get a value from
environ.get('KEY') has the added ability to return the value as its python type. For instance:
import envvars import os envvars.load('/path/to/file/.env') print envvars.get('DEBUG') > False # type boolean print envvars.get('SOME_VARIABLE') > 1348904 # type int # if the .env value of SOME_VARIABLE was "1348904" # Then the return value of envvars.get('SOME_VARIABLE') will be type str