dotenvmodel¶
Type-safe environment configuration with automatic .env file loading¶
dotenvmodel combines Pydantic-style field definitions with intelligent .env file
cascading inspired by Node.js dotenv patterns. Define your config once, get full type
safety, validation, and automatic .env loading — with only one runtime dependency.
Features¶
-
Minimal Dependencies
Only requires
python-dotenv. No heavy frameworks, no transitive dependency trees. -
Type Safety
Full type hint support with automatic type coercion. Your IDE and type checkers (mypy, pyright) understand every field.
-
Rich Type Support
UUID,Decimal,datetime,timedelta,SecretStr,HttpUrl,PostgresDsn,RedisDsn,Json[T],Path, and all standard Python collection types. -
Smart
.envLoading
Automatic cascading of
.env→.env.local→.env.{env}→.env.{env}.localso you can layer base, local, and environment-specific config. -
Validation
Numeric constraints (
ge,le,gt,lt), string constraints (min_length,max_length,regex,starts_with,ends_with), choice validation, customvalidatorhooks, a model-levelpost_loadhook for cross-field validation, stringstripprocessing, and collection size constraints. -
Configuration Documentation
Generate docs in table, markdown, JSON, HTML, and dotenv formats with
describe(). Auto-generate.env.examplefiles withgenerate_env_example(). -
Environment Prefixes
Class-level
env_prefixnamespaces environment variables so multiple config classes coexist without collisions. -
Configuration Reload
Reload configuration at runtime without creating new instances — perfect for responding to
SIGHUPor hot config updates.
Installation¶
Or with uv:
Python 3.12+
dotenvmodel requires Python 3.12 or newer. It has a single runtime dependency:
python-dotenv.
Quick Start¶
from dotenvmodel import DotEnvConfig, Field
class AppConfig(DotEnvConfig):
# Required fields (Pydantic-style)
database_url: str = Field(...)
api_key: str = Field(...)
# Optional with defaults and validation
debug: bool = Field(default=False)
port: int = Field(default=8000, ge=1, le=65535)
workers: int = Field(default=4, ge=1, le=16)
# Collection types
allowed_hosts: list[str] = Field(default_factory=list)
# Load configuration from cascading .env files
config = AppConfig.load(env="dev")
# Access configuration with full type safety and IntelliSense
print(f"Connecting to {config.database_url}") # config.database_url: str
print(f"Running on port {config.port}") # config.port: int
print(f"Debug mode: {config.debug}") # config.debug: bool
# Generate documentation for your configuration
print(AppConfig.describe())
Next Steps¶
- Installation — Set up dotenvmodel in your project
- Quick Start — Go from zero to loaded config in minutes
- Supported Types — UUID, Decimal, datetime, SecretStr, URLs, JSON, and more
- Validation — Numeric, string, choice, and collection constraints
- Loading Configuration —
.envcascading, overrides, and dictionaries - Environment Prefixes — Namespace variables across multiple config classes
- Configuration Documentation —
describe()andgenerate_env_example() - API Reference — Full autogenerated API docs