Tired of writing this in everty project:
PORT = int(os.environ.get("PORT", "8000"))
DEBUG = os.environ.get("DEBUG", "false").lower() == "true"
frozenenv fixes that with one decorator:
from envclass import envclass
@envclass
class Config:
DATABASE_URL: str
PORT: int = 8000
DEBUG: bool = False
ALLOWED_HOSTS: list[str] = []
cfg = Config()
print(cfg.PORT) # 8000 - actual int, not "8000"
Features:
- Zero dependencies: pure Python stdlib
- Type coercion: str, int, float, bool, list[X], Optional[X]
- Frozen - Config is immutable after creation
- Built-in .env support: no python-dotenv needed
- Fails at startup: not buried in runtime
install: pip install frozenenv
GitHub: github.com/hudihi/frozenenv
Full writeup: there-a-cleaner-way-to-handle-environment-variables-and-it-takes-one-decorator
Top comments (0)