Envier
Envier is a Python library for extracting configuration from environment variables in a declarative and 12-factor-app-compliant way. It focuses on type-hinting and clear definition of environment variable mappings to Python objects. The library is currently at version 0.6.1 and is actively maintained with regular releases.
Warnings
- breaking The internal structure and exposure of `HelpInfo` was refactored. Code directly importing `HelpInfo` or relying on its previous class-based implementation might break.
- breaking The Sphinx plugin for auto-generating environment variable documentation was removed. Users who relied on this feature will need to find an alternative documentation generation method.
- gotcha There have been multiple bug fixes (v0.6.1, v0.2.1, v0.2.0) related to the handling of `include` with `namespace` and prefixes for sub-configuration items. Users should thoroughly test configurations involving nested structures and custom prefixes, as these areas have historically shown inconsistencies.
- gotcha A bug affecting the proper handling of `Optional` types in configuration variables was fixed in version 0.5.2.
Install
-
pip install envier
Imports
- Env
from envier import Env
Quickstart
import os
from envier import Env
# Set environment variables for the example
os.environ['MYAPP_DEBUG'] = os.environ.get('MYAPP_DEBUG', 'True')
os.environ['MYAPP_SERVICE_HOST'] = os.environ.get('MYAPP_SERVICE_HOST', 'example.com')
os.environ['MYAPP_SERVICE_PORT'] = os.environ.get('MYAPP_SERVICE_PORT', '8080')
class GlobalConfig(Env):
__prefix__ = "myapp"
debug_mode = Env.var(bool, "debug", default=False)
service_host = Env.var(str, "service.host", default="localhost")
service_port = Env.var(int, "service.port", default=3000)
# Instantiate the configuration
config = GlobalConfig()
print(f"Debug Mode: {config.debug_mode}")
print(f"Service Host: {config.service_host}")
print(f"Service Port: {config.service_port}")
# Expected output if env vars are not set:
# Debug Mode: True
# Service Host: example.com
# Service Port: 8080
# Clean up environment variables (optional, but good for isolated testing)
del os.environ['MYAPP_DEBUG']
del os.environ['MYAPP_SERVICE_HOST']
del os.environ['MYAPP_SERVICE_PORT']