Django "Requested setting ... but settings are not configured" in CI
Django tried to read a setting before it knew which settings module to load. On a runner this means DJANGO_SETTINGS_MODULE was never exported and no settings.configure() ran, so any access to settings.X raises before your code starts.
What this error means
A management command, migration, or test run stops with "ImproperlyConfigured: Requested setting X, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings."
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 is not exported to the step
Your shell locally sets it (or manage.py defaults it), but the CI step runs a bare python or pytest with no environment variable, so Django has no settings module to import.
A script imports Django models before setup
A helper script touches ORM models or django.conf.settings at import time without calling django.setup(), so the settings access fires before configuration.
How to fix it
Export DJANGO_SETTINGS_MODULE in the job
- Point it at your CI settings module (or the default one).
- Set it at the job or step level so every command inherits it.
- Prefer
manage.py, which sets a default, over barepythonfor one-off commands.
env:
DJANGO_SETTINGS_MODULE: myproject.settings.ciCall django.setup() in standalone scripts
A script run outside manage.py must configure Django before touching settings or models.
import os, django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myproject.settings.ci")
django.setup()How to prevent it
- Set DJANGO_SETTINGS_MODULE once at the job level, not per command.
- Run management tasks through manage.py so a default module applies.
- Never access django.conf.settings at module import time in scripts.