How do I stop getting ImportError: Could not import settings 'mofin.settings' when using django with wsgi?


I can't get wsgi to import my settings file for my project 'mofin'.

The list of errors from the apache error log are as follows

mod_wsgi (pid=4001): Exception occurred within WSGI script '/var/www/wsgi-scripts/django.wsgi'.
Traceback (most recent call last):
  File "/usr/lib/python2.5/site-packages/django/core/handlers/", line 228, in __call__
  File "/usr/lib/python2.5/site-packages/django/core/handlers/", line 31, in load_middleware
    for middleware_path in settings.MIDDLEWARE_CLASSES:
  File "/usr/lib/python2.5/site-packages/django/conf/", line 28, in __getattr__
  File "/usr/lib/python2.5/site-packages/django/conf/", line 59, in _import_settings
    self._target = Settings(settings_module)
  File "/usr/lib/python2.5/site-packages/django/conf/", line 94, in __init__
    raise ImportError, "Could not import settings '%s' (Is it on sys.path? Does it have syntax errors?): %s" % (self.SETTINGS_MODULE, e)
ImportError: Could not import settings 'mofin.settings' (Is it on sys.path? Does it have syntax errors?): No module named mofin.settings

I got the "hello world!" wsgi app listed here( to work fine.

The file loads fine with python (runserver|shell|syncdb|test store) as does the application.

Here is my wsgi file:

import os
import sys
print >> sys.stderr, sys.path
os.environ['DJANGO_SETTINGS_MODULE'] = 'mofin.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()

the sys.path as printed in the error log is

['/usr/lib/', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/gtk-2.0', '/home/django/mofin/trunk', '/home/django/mofin/trunk/mofin']

if I open an interactive shell with, sys.path is

['/home/django/mofin/trunk/mofin', '/usr/lib/', '/usr/lib/python2.5', '/usr/lib/python2.5/plat-linux2', '/usr/lib/python2.5/lib-tk', '/usr/lib/python2.5/lib-dynload', '/usr/lib/python2.5/site-packages', '/usr/lib/python2.5/site-packages/gtk-2.0']

My django settings file looks like this: # Django settings for mofin project.

DEBUG = True

    # ('Dan xxxx', ''),


DATABASE_ENGINE = 'mysql'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
DATABASE_NAME = 'mofin'             # Or path to database file if using sqlite3.
DATABASE_USER = 'aaaaaa'             # Not used with sqlite3.
DATABASE_PASSWORD = 'bbbbbb'         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.

# Local time zone for this installation. Choices can be found here:
# although not all choices may be available on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Europe/London'

# Language code for this installation. All choices can be found here:


# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/"
MEDIA_ROOT = '/home/django/media/'

# URL that handles the media served from MEDIA_ROOT. Make sure to use a
# trailing slash if there is a path component (optional in other cases).
# Examples: "", ""

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "", "/media/".
ADMIN_MEDIA_PREFIX = '/admin_media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

# List of callables that know how to import templates from various sources.
#     'django.template.loaders.eggs.load_template_source',


ROOT_URLCONF = 'mofin.urls'

    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.

5/15/2013 6:01:43 AM

This can also happen if you have an application (subdirectory to the project with an init file in it) named the same thing as the project. Your file may be in your project folder, but it seems that a part of the django system looks first for a module inside the project by the same name as the project and when it can't find a in there, it fails with a misleading message.



---uniquename1  (problem, rename this to some other unique name)

Just something else to check for anyone else having this problem. Applies to Django 1.3 and probably others.

1/20/2015 4:14:34 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow