Main Ecosystems
HomePython CorePandas ReferenceNumPy ScientificFastAPI & PydanticDjango Enterprise
More Ecosystems
Environment & SetupRequests & HTTPAsyncIO ConcurrencyObject-Oriented OOPPyTorch Deep LearningScikit-Learn MLFlask FrameworkWeb ScrapingDatabase & ORMDevOps & Docker

Django ImproperlyConfigured: Requested setting INSTALLED_APPS, but settings are not configured

Verified FixPython 3.10+Django 4.2+ / 5.0+Silo: django

Quick Fix / Solution Rapide

Set the DJANGO_SETTINGS_MODULE environment variable and call django.setup() before importing any Django models in standalone scripts.

Root Cause Analysis

This error occurs when Python executes a standalone script, unit test, or Celery task that imports Django components (such as models, forms, or settings), but Django's application registry has not been initialized with project settings.

1. Running Scripts Outside manage.py

manage.py sets DJANGO_SETTINGS_MODULE and calls django.setup() automatically. When running python scripts/import_data.py directly, Python does not know which settings.py file to load.

2. Importing Models Before django.setup()

If django.setup() is called after importing from myapp.models import User, the model import executes at module load time before INSTALLED_APPS is populated, raising ImproperlyConfigured.

3. Celery / Pytest Configuration Omissions

Running pytest without django-pytest or running Celery workers without setting app.config_from_object('django.conf:settings').

4. Jupyter Notebook Initialization

Importing Django modules inside Jupyter notebooks without executing django.setup() in the first cell.

Reproduction Code (MCVE)

Example: Bug Reproduction
# Simulating Django ImproperlyConfigured error when settings are accessed uninitialized
class ImproperlyConfigured(Exception):
    pass

class UnconfiguredSettings:
    def __getattr__(self, name):
        raise ImproperlyConfigured(
            f'Requested setting {name}, but settings are not configured. '
            'You must either define the environment variable DJANGO_SETTINGS_MODULE '
            'or call settings.configure() before accessing settings.'
        )

settings = UnconfiguredSettings()
apps = settings.INSTALLED_APPS

Solution 1: Configure Environment and Call django.setup() in Scripts

Initialize Django settings before any model or application imports in standalone scripts.

Example: Recommended Solution
import os

# 1. Set environment variable pointing to your project settings
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myproject.settings')

# 2. Initialize Django context (simulated)
print('Django settings initialized successfully for standalone script execution.')

Solution 2: Use Custom Django Management Commands

Instead of raw standalone scripts, create a custom management command in management/commands/ which runs within Django's context natively.

Example: Alternative Solution
print('Create: myapp/management/commands/import_data.py')
print('from django.core.management.base import BaseCommand')
print('class Command(BaseCommand):')
print('    def handle(self, *args, **options):')
print('        print("Command executed with full Django context!")')
print('\nExecute with: python manage.py import_data')

A common mistake is placing import django; django.setup() after from myapp.models import Item. Because Python imports execute top-to-bottom, the model import will fail before django.setup() is reached. Always place django.setup() before model imports. Edge cases occur with Pytest: configure DJANGO_SETTINGS_MODULE = myproject.settings in pytest.ini. Contrast this error with django.core.exceptions.AppRegistryNotReady, which occurs when importing models during Django's settings compilation phase.