Configuring apps with “.env” files

Syntactic sugar for compiled code



Assumed audience:

People who run code in multiple places

This is mostly python tips for now, but I want this because it is cross-platform potentially. A part of a configuring ML.

There are many, many python tools to load environment variables from local files. A good place to find generic resources on that is “Twelve-Factor App” configuration. But that includes lots of extraneous stuff for web people in particular and I do not care about that, I just want the environment config part.

One system I have used is dotenv. dotenv allows easy configuration through OS environment variables or text files in the parent directory.

PRO-TIP: there are lots of packages with similar names but dissimilar functions.

pip install python-dotenv # or
conda install -c conda-forge python-dotenv

Also very similar, henriquebastos/python-decouple. sloria/environs: simplified environment variable parsing Dynaconf is more sophisticated, and comes closer to a full configuration system like hydra.

Let us imagine we are using basic dotenv for now for concreteness Then we can be indifferent to whether files came from an FS config or an environment variable.

from dotenv import load_dotenv
load_dotenv()  # take environment variables from .env.
# Code of your application, which uses environment variables (e.g. from `os.environ` or
# `os.getenv`) as if they came from the actual environment.
DATA_PATH = os.path.expandvars('$DATA_PATH/$DATA_FILE')

There is a CLI too; its most useful feature is executing arbitrary stuff with the correct environment variable set.

pip install "python-dotenv[cli]"
dotenv run my_cool_script.py

AFAICS this should mean that dotenv is not restricted to python; those .env files can be used in any language. This only works for running python scripts AFAICT.


No comments yet. Why not leave one?

GitHub-flavored Markdown & a sane subset of HTML is supported.