Python EnvCFG

0.2.0 · abandoned · verified Fri Apr 17

Python EnvCFG (python-envcfg) provides a 'magic module' for accessing environment variables as attributes, automatically converting them to common Python types (str, int, float, bool). It also supports loading configuration from YAML files. The current version is 0.2.0, released in 2017, and the project appears to be no longer actively maintained.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to access environment variables using the `env` magic module, including automatic type conversion and providing default values for unset variables. Note that environment variables must be set in `os.environ` before `envcfg` processes them.

import os
from envcfg import env

# Simulate setting environment variables for demonstration
os.environ['DATABASE_URL'] = 'postgres://user:password@host:port/database'
os.environ['DEBUG'] = 'true'
os.environ['PORT'] = '8000'

print(f"Database URL: {env.DATABASE_URL} (type: {type(env.DATABASE_URL)})")
print(f"Debug Mode: {env.DEBUG} (type: {type(env.DEBUG)})") # Auto-converts 'true' to True
print(f"Port Number: {env.PORT} (type: {type(env.PORT)})") # Auto-converts '8000' to 8000

# Accessing an undefined variable with a default
app_env = env.APP_ENV('development')
print(f"App Environment: {app_env} (type: {type(app_env)})")

# Example of retrieving an explicitly string value:
string_port = env.PORT('default_string_port') # Ensures string type
print(f"Port as String: {string_port} (type: {type(string_port)})")

view raw JSON →