Skip to main content

philiprehberger-env-file

Tests PyPI version Last updated

.env file parser with interpolation and multi-file support.

Installation

pip install philiprehberger-env-file

Usage

Load into Environment

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)

Parse Without Modifying Environment

from philiprehberger_env_file import parse_env_file

config = parse_env_file(".env")
print(config["DATABASE_URL"])

Writing .env files

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.

Merging without loading

from philiprehberger_env_file import merge_env

# Later files override earlier ones; os.environ is untouched
config = merge_env(".env", ".env.local")

Supported Syntax

# 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

API

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

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

⭐ Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Release files for philiprehberger-env-file 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for philiprehberger-env-file 0.2.0
File Size Uploaded
philiprehberger_env_file-0.2.0.tar.gz 179.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for philiprehberger-env-file 0.2.0
File Interpreter ABI Platform
philiprehberger_env_file-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 185.4 kB

Release files / philiprehberger_env_file-0.2.0.tar.gz

Download URL philiprehberger_env_file-0.2.0.tar.gz
Size 179.8 kB
Tags Source
SHA-256 checksum
How to use checksums
4a29929a64c7bc8417e586246765a2fe2f4ecc01d9c595ff511bdf9de982393d
BLAKE2b-256 checksum
How to use checksums
566d1e4c6e24c08959afac1706fbd5ae5483e4a5eb028e8f602a1174ae37e5d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release files / philiprehberger_env_file-0.2.0-py3-none-any.whl

Download URL philiprehberger_env_file-0.2.0-py3-none-any.whl
Size 5.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
887381f92f67abfb374a167021d56ac5408b44d2a0bcdcbe3864e2a1e371615a
BLAKE2b-256 checksum
How to use checksums
5db0b8fe308b4ef7741642b6b69c2360b9fa90b75e7efa0683c0fab678c10b35
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.13

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.6

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page