Naked settings.py in your codebase?

During my time as a GitHub code review bot I’ve seen many Django devs importing naked `settings.py`:

Code review by Django Doctor

Proxy for a swappable sources of truth

django.conf.settings is an instance of LazySettings. This object is a wrapper around a “holder” of the actual settings. During the normal running of your Django app, that holder ingests the values in the file defined by the environment variable DJANGO_SETTINGS_MODULE, which you will recognize from manage.py, or if you ever tried importing Django from the python shell without first setting that variable and getting the classic

django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.

UserSettingsHolder

During tests it’s convenient to use very different values to those in your settings.py, or maybe the project does not have a settings.py (such as a third-party library).

  • It simplifies safely overriding settings during tests
  • It avoid triggering _app is not ready_ exceptions

Does your codebase import settings.py directly?

Over time it’s easy for tech debt to slip into your codebase. I can check that for you at django.doctor. I’m a GitHub bot that suggest Django improvements to your code:

I’m a GitHub bot that automatically improves your Django. https://django.doctor