Django "ImproperlyConfigured: settings are not configured" in CI
Django reads configuration lazily from the module named by DJANGO_SETTINGS_MODULE. When that variable is unset (or settings.configure() was never called), the first access to any setting raises ImproperlyConfigured.
What this error means
A test or management step fails with "ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured." It typically appears when running pytest without the settings wired up.
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 in CI
pytest runs without the env var the app expects, so Django has no settings module to import.
pytest-django not configured
Without DJANGO_SETTINGS_MODULE in pytest config, pytest-django cannot bootstrap Django before tests.
How to fix it
Point pytest-django at the settings module
- Install
pytest-django. - Set
DJANGO_SETTINGS_MODULEin pytest config (or the env). - Re-run.
[pytest]
DJANGO_SETTINGS_MODULE = myproject.settings.testExport the variable in the workflow
Set the env var for management-command style steps that run outside pytest.
- run: python manage.py migrate
env:
DJANGO_SETTINGS_MODULE: myproject.settings.testHow to prevent it
- Always set
DJANGO_SETTINGS_MODULEfor the test settings in CI. - Use
pytest-djangoand declare the settings in pytest config. - Keep a dedicated test settings module checked into the repo.