django-getenv – use environment variables in your Django settings

So I made a thing for using environment variables within your Django settings: django-getenv.

Although the code itself is trivial (and, to be honest, not coupled with Django in any way), because I was re-using the same code in multiple projects I decided – if only to make my life easier – to made it into a standalone module and package.

Enough of the what, let us get onto the why.

I’m a big fan of The Twelve-Factor App, and although I can’t or don’t always follow its tenets in every app I write, I do my best.

One of the tenets is “Store config in the environment”:

The twelve-factor app stores config in environment variables (often shortened to env vars or env). Env vars are easy to change between deploys without changing any code; unlike config files, there is little chance of them being checked into the code repo accidentally; and unlike custom config files, or other config mechanisms such as Java System Properties, they are a language- and OS-agnostic standard.

Although Django has a range of configuration options, it does not lend itself well to using environment variables out of the box.

Jacob Kaplan-Moss has addressed part of this with django-dotenv which lets you use a Foreman-style .env file to populate your environment with the settings contained within that file.

django-getenv is another piece of the puzzle, helping you use those environment variables in your Django project settings. As well as simplifying accessing these variables, it will also convert boolean, integer and float values to their native Python types.

The module is BSD (3-clause) licensed, available on Pypi, and the project is hosted on Github.