Managing environment-specific Python settings

How do you deal with settings changes that manipulate Python for different environments? That’s a question that came up as I was doing my Django Twitch stream (shameless plug :smile:) tonight.

In my current project configuration, I have multiple settings files for different environments like settings/development.py, settings/production.py, and so on which inherit settings from a settings/base.py file. I’m trying to switch to a scheme that only uses a single settings file and reads stuff externally. I happened to select goodconf, but you can totally do something similar with django-environ.

My trouble with this switch is that I don’t know what to do when one of my other settings files manipulated Python. For instance, here is settings/development.py:

from settings.base import *  # noqa

INTERNAL_IPS = ("127.0.0.1",)
INSTALLED_APPS += ("debug_toolbar",)  # noqa
MIDDLEWARE.insert(0, "debug_toolbar.middleware.DebugToolbarMiddleware")  # noqa

The only substantial change that this settings file is doing is configuring django-debug-toolbar. To do that, I’m monkeying with INSTALLED_APPS and MIDDLEWARE at the Python level (i.e., manipulating lists).

If you use a tool like django-environ, how do you like to deal with these environment-specific customizations? It looks like django-configurations might support this with setup methods. I’m interested to see other techniques that people use.

I only use two settings files in my projects.: settings.py and test_settings.py. My main settings.py file uses django-environ and has good, local defaults which are overrideable with environment variables.

While I don’t use debug_toolbar, I would do something like:

DEBUG = env.bool("DJANGO_DEBUG", default=False)

if DEBUG:
    INTERNAL_IPS = ("127.0.0.1",)
    INSTALLED_APPS.append("debug_toolbar")  # noqa
    MIDDLEWARE.append("debug_toolbar.middleware.DebugToolbarMiddleware")  # noqa

My test_settings.py is disabled migrations, using a faster password hasher, disables logging, and basically a bunch of other tricks to make my testsuite run as fast as possible.

Your production settings shouldn’t be in git so set them via environment variables and/or SECRETS with your hosting provider.

2 Likes

I strongly agree with Jeff’s approach. Using environment variables like this is much easier, to me, to reason about.

1 Like