.env file parser with interpolation and multi-file support
pip install philiprehberger-env-file.env file parser with interpolation and multi-file support.
pip install philiprehberger-env-file
from philiprehberger_env_file import load_env
# Load .env (default)
load_env()
# Load multiple files (later files override earlier)
load_env(".env", ".env.local")
# Don't override existing env vars
load_env(override=False)
from philiprehberger_env_file import parse_env_file
config = parse_env_file(".env")
print(config["DATABASE_URL"])
from philiprehberger_env_file import dump_env
dump_env(
{"DATABASE_URL": "postgres://localhost/db", "DEBUG": "1"},
".env",
)
Values containing whitespace, #, ", or ' are automatically
quoted. Pass quote=False to disable.
from philiprehberger_env_file import merge_env
# Later files override earlier ones; os.environ is untouched
config = merge_env(".env", ".env.local")
# Comments
KEY=value
export KEY=value
# Quoted values
SINGLE='no interpolation here'
DOUBLE="supports\nnewlines"
# Variable interpolation
BASE_URL=https://api.example.com
API_URL=${BASE_URL}/v1
# Empty values
EMPTY_VAR=
# Inline comments (unquoted values only)
HOST=localhost # this is a comment
| Function / Class | Description |
|---|---|
load_env(*paths, override=True) | Load .env files into os.environ, returns dict of loaded vars |
parse_env_file(path) | Parse a .env file, returns dict without modifying environment |
dump_env(data, path, quote=True) | Write a dict to a .env file, auto-quoting values that need it |
merge_env(*paths) | Parse multiple .env files into one dict; missing files are skipped, os.environ is not modified |
pip install -e .
python -m pytest tests/ -v
If you find this project useful: