Interview Prep/Python for DevOps/Best practices & rapid-fire commands

How do you handle configuration in a Python tool?

IntermediateConceptBest practices & rapid-fire commands
Answer

Layer it, with clear precedence: built-in defaults, then a config file (YAML/TOML), then environment variables, then CLI flags (highest). Validate the merged result at startup with pydantic or explicit checks and fail with a clear message. Use tomllib (standard library since 3.11) to read TOML. Keep secrets out of config files; reference a secret name and resolve it at runtime. Twelve-factor services usually need only env vars (pydantic-settings reads them into a typed model).

All Python for DevOps questions Roadmap: Python for automation

More on Best practices & rapid-fire commands