/errors/django/improperlyconfigured-settings-not-configured
ImproperlyConfigured: Requested setting …, but settings are not configured
verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated 2026-10-20
- tool
- Django
- exit code
- 1
- versions
- Django 4.2 – 6.1
- os
- any
Django raises django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured when something reads django.conf.settings before Django knows which settings module to use. manage.py, wsgi.py and asgi.py set DJANGO_SETTINGS_MODULE for you; plain scripts, notebooks and test runners do not.
Reproduce the error
python -c "from django.conf import settings; print(settings.DEBUG)" 2>&1 | tail -1; exit "${PIPESTATUS[0]}"Output · exit 1
django.core.exceptions.ImproperlyConfigured: Requested setting DEBUG, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
Fix: point Django at your settings
mkdir -p mysite && printf 'SECRET_KEY = "dev"\nDEBUG = True\nUSE_TZ = True\n' > mysite/settings.py && touch mysite/__init__.py
DJANGO_SETTINGS_MODULE=mysite.settings python -c "from django.conf import settings; print('DEBUG =', settings.DEBUG)"Output
DEBUG = True
Standalone scripts that use models
Set the variable and call django.setup() before importing anything that touches models:
import os
import django
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")
django.setup()
from polls.models import Question # import models only after setup()
print(Question.objects.count())pytest
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "mysite.settings"This option is read by the pytest-django plugin.
Sources
Related pages
config · djangoDjango ALLOWED_HOSTS: values, wildcards and DisallowedHost errorsALLOWED_HOSTS lists the host names a Django site may serve. Any other Host header raises DisallowedHost, a 400 error. Use exact names or .example.com.commands · djangopython manage.py makemigrations and migrate: create and apply migrationsmakemigrations turns model changes into migration files and migrate applies them to the database. Preview SQL with sqlmigrate, check state with showmigrations.cheat sheets · djangoDjango manage.py cheat sheet: project, migrations, users, testingDjango manage.py cheat sheet: startproject and startapp, makemigrations and migrate, createsuperuser, runserver, shell, check, test, collectstatic and dumpdata.