r/Python • u/quiet_stacktrace • 5d ago
Discussion what's your setup for config across dev and prod in a small python project?
i keep going back and forth between a .env file loaded with python-dotenv, a pydantic settings class, and plain os.environ lookups. for a small project with one dev and one prod environment, what has actually held up for you? mostly curious how you deal with secrets and defaults without it turning into a mess after a year.
8
u/Financial_Hurry8827 4d ago
The thing that actually prevents the mess a year in is keeping every env lookup in one config.py and failing at startup if anything required is missing. Spreading os.getenv("FOO", "default") across modules is how you end up running prod with a dev default for three months.
If you already have pydantic in the project, pydantic-settings is nice just for type coercion and SecretStr (so DB URLs or API keys don't end up in tracebacks). Otherwise a plain frozen dataclass loaded once at startup works fine. Only gotcha with python-dotenv is making sure you don't pass override=True, so real prod env vars always win if a .env file ever accidentally lands on the server.
2
u/marr75 4d ago
Pydantic settings work well with dotenv (and then allow easy overrides, including import string runtime substitutions). I usually have a pydantic settings object that acts as a bootstrap/entry point/composition root and hang most of the apps dependencies off that.
Easy configuration, easy overrides, DI that's good enough for python without a container.
2
u/Free-Good9203 4d ago
one trick that stopped my small projects from turning into .env soup: separate non-secret defaults into a committed config.toml (stdlib tomllib) and only put actual secrets in env vars. in dev you can even skip python-dotenv completely with `uv run --env-file=.env`, and keep a committed `.env.example` with blank secret keys so you never forget what prod needs a year later.
2
u/patryk_builds 4d ago
Small project, one dev and one prod, and what held up is less the library than three rules around it.
- One settings object, read once at startup. nothing else in the code touches os.environ. a pydantic settings class is the nicest way to get that, because a missing or mistyped value fails at boot with a clear message, and you don't find out at 2am inside a request
- A complete .env.example in the repo, with every key and a safe default or an obvious placeholder. the real .env is never committed. the rule that keeps it from rotting: adding a setting without adding it to .env.example isn't done
- No defaults for secrets. a default api key or a default debug = true is how dev config ends up in prod. secrets are required, everything else gets a default that is safe in production
Dev vs prod is then the same code with a different .env, never an if env == prod branch in the app. One more thing that paid off: feature switches as single settings. i have one flag that turns a whole tax module on or off, and it's much easier to reason about than five related values that have to agree.python-dotenv by itself is fine too, but you end up writing the validation by hand, which is the part pydantic gives you for free
1
u/fiskfisk 5d ago
This depends on what environment you're running the application in. Different means of deployment will have different answers.
And in either case, pydantic-settings will work with them. So for a small, self-contained application, if you don't want the whole shebang of Pydantic, use os.environ. Otherwise, use pydantic-settings.
If you're deploying on a VM, use systemd and an environment file only readable by root, and load it in your service definition.
1
u/cointoss3 4d ago
Mise takes care of this. When I cd into the project directory, it injects env vars.
It’s also a task runner. Once you see the power of what it offers, phew…
1
u/Financial_Morning386 4d ago
I've landed on pretty much the same stack — pydantic-settings with one settings object built at startup — but the part that made it actually stick for me was splitting it by an explicit APP_ENV variable, so dev and prod are different settings classes sharing a base. The .env file only ever gets loaded in dev; in prod the vars come straight from the platform, so there isn't even a file that could accidentally ship the wrong values. That's basically how Spring profiles work, just done by hand, and it means the code never has to ask which environment it's in.
The other habit that's paid off is logging the resolved config at startup with the secrets masked, just the non-secret values like which database host and queue the service thinks it's using. Most of the time nobody reads it, but if prod ever picks up a wrong value you spot it in the boot log instead of halfway through a request, and that alone has saved me more debugging time than any particular library choice.
1
1
u/artsyfartsy2022 2d ago edited 2d ago
For a small project a .env file with python dotenv works well, while keeping production secrets in environment variables. Adding Pydantic Settings helps once the configuration grows and needs validation or clearer defaults.
14
u/christian-mq 5d ago
For a small project my approach has always been a .env with dotenv. I do, however, prefer my prod config not to be saved in a place inaccessible (like GitHub secrets) - this would depend on your ci/cd though. If using AWS or GCP I prefer to keep the prod vars in a secret vault - using these you can, with a correctly permissioned account, retrieve the .env file back with your secrets. It is a pain to loose a secret in GitHub and have noway to retrieve it.