Envs

1.4 · active · verified Sat Apr 11

Envs is a Python library (v1.4) designed for easy and typed access to environment variables. It automatically handles the parsing of environment variable values into various Python types, including strings, booleans, lists, tuples, integers, floats, and dictionaries. The library was last released in December 2021, suggesting a low-to-moderate release cadence, with updates as needed.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates both direct access to environment variables using `env()` and defining a structured settings class with type hints by inheriting from `envs.Env`. It showcases how `envs` automatically casts values to the specified Python types and provides default values for missing variables.

import os
from envs import env, Env

os.environ['APP_DEBUG'] = 'true'
os.environ['API_KEY'] = 'your_api_key_123'
os.environ['THRESHOLD'] = '100'
os.environ['FEATURE_FLAGS'] = 'featureA,featureB'
os.environ['DB_SETTINGS'] = '{"host": "localhost", "port": 5432}'

class AppSettings(Env):
    debug: bool = env('APP_DEBUG', False)
    api_key: str = env('API_KEY')
    threshold: int = env('THRESHOLD', 50)
    feature_flags: list[str] = env('FEATURE_FLAGS', [])
    db_settings: dict = env('DB_SETTINGS', {})

settings = AppSettings()

print(f"Debug mode: {settings.debug} (type: {type(settings.debug)})")
print(f"API Key: {settings.api_key} (type: {type(settings.api_key)})")
print(f"Threshold: {settings.threshold} (type: {type(settings.threshold)})")
print(f"Feature Flags: {settings.feature_flags} (type: {type(settings.feature_flags)})")
print(f"DB Settings: {settings.db_settings} (type: {type(settings.db_settings)})")

# Direct access without a schema
direct_api_key = env('API_KEY', '')
print(f"Direct API Key: {direct_api_key} (type: {type(direct_api_key)})")

view raw JSON →