PYTHON Contents

Config Management (Env, Files, Defaults)

Load configuration at startup, validate required values, and avoid implicit defaults that create production-only surprises.

On this page

Config is Runtime Policy

Configuration should be explicit and validated once at startup. Production incidents often come from missing or wrong config.

Fail Fast Parsing

import os

def require_env(name: str) -> str:
    v = os.getenv(name)
    if not v:
        raise RuntimeError(f"missing env: {name}")
    return v

DATABASE_URL = require_env("DATABASE_URL")
TIMEOUT_SECONDS = int(os.getenv("TIMEOUT_SECONDS", "10"))

Operational Checklist

  • Validate required config at startup; crash early if missing.
  • Make units explicit (seconds, bytes) and document them.
  • Keep defaults conservative and visible in code.

Failure Modes

  • Implicit defaults: missing env var silently changes behavior.
  • Config drift: staging/prod differ without being tracked.