Skip to content
Latchkey

pytest-django "Settings Are Not Configured" / DJANGO_SETTINGS_MODULE

pytest-django needs to know which Django settings module to load. Without DJANGO_SETTINGS_MODULE (or the DJANGO_SETTINGS_MODULE/ds pytest option), Django raises ImproperlyConfigured as soon as a setting is read.

What this error means

Collection or the first test fails with django.core.exceptions.ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured. It happens before test logic runs, because Django has no settings module to load.

pytest output
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.

Common causes

DJANGO_SETTINGS_MODULE not set

pytest-django reads the settings module from the DJANGO_SETTINGS_MODULE env var or pytest’s ds/DJANGO_SETTINGS_MODULE option. If neither is set, Django cannot configure.

Wrong path to the settings module

A dotted path that does not import (typo, missing package on sys.path, src-layout) leaves settings unconfigured.

How to fix it

Declare the settings module in pytest config

Point pytest-django at your settings so it configures Django before collection.

pyproject.toml
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "myproject.settings.test"

Or set the environment variable in CI

Terminal
export DJANGO_SETTINGS_MODULE=myproject.settings.test
pytest

How to prevent it

  • Set DJANGO_SETTINGS_MODULE (or pytest ds) for the test settings.
  • Keep a dedicated test settings module and reference it consistently.
  • Install the project so the settings dotted path resolves on the runner.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →