Simple layered loading of env files
Project description
envfiles: Simple layered loading of env files
So you've set up your app to configure itself from environment variables, awesome, well done!
Now all you need to do is to make sure all of them are present before running your app. Good thing we have env files, right? But wait, they can't be the same all the time, they're slightly different for tests, Docker, and for local development... that's annoying.
No biggie though, we can just create several env files and load them... But that would fail in prod because the env files won't be there... We could load them only if they exist but that's kinda hacky...
Wouldn't it be neat if we could layer env files? Imagine a base.env
:
CACHE_ENABLED=1
DATABASE_HOST=localhost
DATABASE_NAME=myapp
A test.env
that overrides it:
# >> base.env
CACHE_ENABLED=0
DATABASE_NAME=myapp_test
Note the first line # >> base.env
, the # >>
is an arbitrary prefix, the
base.env
is a path relative to the file being read.
Then point envfiles to the file you want to load and let it resolve the variables.
For example, you could define one environment variable, ENV_FILE
for instance,
containing a relative path to the env file you want to load:
import envfiles
env_vars = envfiles.load_env_files(os.getenv("ENV_FILE"))
assert env_vars == {
"CACHE_ENABLED": "0",
"DATABASE_HOST": "localhost",
"DATABASE_NAME": "myapp_test",
}
Note envfiles
will not mess with your os.environ
, or attempt parsing or
(de)serializing variables. The output is a dictionary of strings to strings,
what you do with it is entirely your business. Typically you would combine it
with os.environ
and pass it to your app's configuration solution.
For example, if you're using
environ-config
💚
import environ
import envfiles
from settings import MyConfig # your environ-config Config class
env_vars = envfiles.load(os.getenv("ENV_FILE"))
env_vars.update(os.environ) # actual environment variables have preference
config = environ.to_config(MyConfig, environ=env_vars)
Why?
Frustration, mostly. I've had this issue more times than I can count. I've tried
different libraries but none of them supported layering or sort of did but
messed with the os.environ
in surprising ways.
The idea behind envfiles
is to declaratively define how the env files are loaded
and let you use whatever configuration solution you want.
Configuration is the first thing your app does, it should be as quick and straightforward as possible.
Alternatives
If envfiles
is not what you were looking for here are some other options you
may want to consider, all of them more mature and featured than envfiles
:
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file envfiles-0.1.1.tar.gz
.
File metadata
- Download URL: envfiles-0.1.1.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f4ed2493ff71c83a73ee53b58cf034b1d770116f26b1ea4f5b0d2faf1c330a6 |
|
MD5 | 8ea0c0d1a277cbb1ec951ecd8fec70b0 |
|
BLAKE2b-256 | e94ceedf2b8faf88101eb9aa5c3d4f45a5fbe5eb24bf46df971ae72d2112d7ba |
File details
Details for the file envfiles-0.1.1-py3-none-any.whl
.
File metadata
- Download URL: envfiles-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: python-requests/2.27.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3800bee72aa4a4dd2fc46b60b9b37057299c7c7e5453b1dcbbd07d00f2689eac |
|
MD5 | 3bb1c71c458e8d340b88ec9d3e838ab4 |
|
BLAKE2b-256 | 0249089e90339033755a56fde6e4df479c19e1df42542381ecadbe0b778c6610 |