Skip to main content

Simple layered loading of env files for your 12-factor app.

Project description

envfiles: Simple layered loading of env files for your 12-factor app

You've set up your app to configure itself from environment variables, awesome, well done. Now all you need to do is to export all of them before running your app. Oh, and also they need to be slightly different in Docker, and tests, and for local development...

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 define one environment variable, for example ENV_FILE, 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.

You can simply update os.environ with the result of envfiles and let your configuration library/code pick it up. Do note that if you do this the actual environment variables will be overridden by the result of envfiles.

Instead, I suggest updating the results with os.environ and passing it to 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)
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 just read layered env files 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envfiles-0.1.0.dev0.tar.gz (8.9 kB view details)

Uploaded Source

Built Distribution

envfiles-0.1.0.dev0-py2.py3-none-any.whl (8.4 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file envfiles-0.1.0.dev0.tar.gz.

File metadata

  • Download URL: envfiles-0.1.0.dev0.tar.gz
  • Upload date:
  • Size: 8.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for envfiles-0.1.0.dev0.tar.gz
Algorithm Hash digest
SHA256 6da491e730adb5441fb98204cd0e41da9749c22a8a531c9cea97d1e1f9d49332
MD5 4c4235da7449399e4907bed80887e727
BLAKE2b-256 a6df453e82cd17aaacd167a1028a3cf0e0d90d701f9a04d27efb885d28b090c9

See more details on using hashes here.

File details

Details for the file envfiles-0.1.0.dev0-py2.py3-none-any.whl.

File metadata

  • Download URL: envfiles-0.1.0.dev0-py2.py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4

File hashes

Hashes for envfiles-0.1.0.dev0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f12094a92da78b29b4d64baafbd2d87663acca81dd410ec7968c545a49027194
MD5 e098766bd0c1b4789d16a3ce3015a54e
BLAKE2b-256 bbf6dba5669f0db020a29bd17998cf3f8cc50f3d145216370848a6527917616c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page