Zamphatta Zamphatta - 1 month ago 8
Python Question

Django DB Settings 'Improperly Configured' Error

Django (1.5) is workin' fine for me, but when I fire up the Python interpreter (Python 3) to check some things, I get the weirdest error when I try importing -

from django.contrib.auth.models import User

Traceback (most recent call last):
File "/usr/local/lib/python3.2/dist-packages/django/conf/", line 36, in _setup
settings_module = os.environ[ENVIRONMENT_VARIABLE]
File "/usr/lib/python3.2/", line 450, in __getitem__
value = self._data[self.encodekey(key)]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.2/dist-packages/django/contrib/auth/", line 8, in <module>
from django.db import models
File "/usr/local/lib/python3.2/dist-packages/django/db/", line 11, in <module>
if settings.DATABASES and DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/usr/local/lib/python3.2/dist-packages/django/conf/", line 52, in __getattr__
File "/usr/local/lib/python3.2/dist-packages/django/conf/", line 45, in _setup

django.core.exceptions.ImproperlyConfigured: Requested setting DATABASES,
but settings are not configured. You must either define the environment
variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.

How could it be improperly configured, when it works fine outside the Python interpreter? In my Django settings, the
settings are:

'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
'NAME': 'django_db', # Or path to database file if using sqlite3.
# The following settings are not used with sqlite3:
'USER': 'zamphatta',
'PASSWORD': 'mypassword91',
'HOST': '', # Empty for localhost through domain sockets or '' for localhost through TCP.
'PORT': '', # Set to empty string for default.
} is this improperly configured?


You can't just fire up python and check things, django doesn't know what project you want to work on. You have to do one of these things:

  • Use python shell
  • Use shell --settings=mysite.settings (or whatever settings module you use)
  • Set DJANGO_SETTINGS_MODULE environment variable in your OS to mysite.settings
  • (This is removed in django 1.6) Use setup_environ in the python interpreter:

    from import setup_environ
    from mysite import settings

Naturally, the first way is the easiest.