Naked settings.py in your codebase?
Tags: django, python, codequality, github
Published: 2020-11-08 20:36:53

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

![Code review by django.doctor ](https://dev-to-uploads.s3.amazonaws.com/i/85shaymb7c1p1jph4ug5.png)

Django best practice tells us importing `settings.py` is bad and we should instead use `from django.conf import settings`, but why?

A simple answer is naked `settings.py` will not have the Django [default values](https://github.com/django/django/blob/master/django/conf/global_settings.py) and can trigger a race condition, but there is more to it than that.

Overriding settings in tests is simplified too when using `django.conf.settings` as we can then use Django's [`modify_settings`](https://docs.djangoproject.com/en/3.1/topics/testing/tools/#django.test.modify_settings) and django-pytest's [`settings`](https://pytest-django.readthedocs.io/en/latest/helpers.html#settings) fixture. This helps avoid unmaintainable flaky tests as one test's changes will not affect another because changes are reset after the test finishes.

To understand how that works we need to look deeper at what `django.conf.settings` is exactly.

## Proxy for a swappable sources of truth
`django.conf.settings` is an instance of [`LazySettings`](https://github.com/django/django/blob/master/django/conf/__init__.py#L49). 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`](https://github.com/django/django/blob/84ca7b860223d9211976be1a179c727784defc7a/tests/i18n/sampleproject/manage.py#L8), 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.`


As the name "lazy" implies, the values from `DJANGO_SETTINGS_MODULE` are not evaluated [until they are interacted with](https://github.com/django/django/blob/master/django/conf/__init__.py#L82). This is how we can import settings throughout our codebase with confidence that we will not get an error that the app is not ready yet.

So we can see, `django.conf.settings` is not the same thing as the `settings.py` file: `django.conf.settings` is a _normally_ a proxy around your `settings.py` file, but not always.

## 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).

For these cases we can [explicitly call](https://pytest-django.readthedocs.io/en/latest/configuring_django.html#using-django-conf-settings-configure) `settings.cofigure()`. When that happens the `django.conf.settings`'s holder will be a [`UserSettingsHolder`](https://github.com/django/django/blob/84ca7b860223d9211976be1a179c727784defc7a/django/conf/__init__.py#L229), and not interact with `settings.py` at all.

So there are a few great reasons to use `from django.conf import settings` instead of importing the `settings.py` directly:

- You get Django's default values set
- It simplifies safely overriding settings during tests
- It avoid triggering _app is not ready_ exceptions

Beware though that lowercase values in your `settings.py` are not exposed to `django.conf.settings`, but you can store those values in some other files so not a huge loss.

### 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](https://django.doctor), or can [review your GitHub PRs](https://github.com/marketplace/django-doctor/):

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/d10epulva9d373jgldhu.png)

Or try out [Django refactor challenges](https://django.doctor/challenge).