Question

For some reason, I am getting an ImproperlyConfigured error on account of custom middleware.

[Wed Nov 07 20:47:07 2012] [error] [client 158.130.107.158] File does not exist: /home/davidxu/public_html/favicon.ico
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158] mod_wsgi (pid=24114): Exception occurred processing WSGI script '/home/davidxu/dev/Penn-Course-Review-api/api/django.wsgi'.
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158] Traceback (most recent call last):
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 219, in __call__
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158]     self.load_middleware()
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158]   File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 47, in load_middleware
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158]     raise exceptions.ImproperlyConfigured('Error importing middleware %s: "%s"' % (mw_module, e))
[Wed Nov 07 20:47:09 2012] [error] [client 158.130.107.158] ImproperlyConfigured: Error importing middleware api.apiconsumer.authenticate: "No module named api.apiconsumer.authenticate"

For reference, here are the relevant parts of the settings.py file:

MIDDLEWARE_CLASSES = ( 
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.cache.UpdateCacheMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.cache.FetchFromCacheMiddleware',
    'api.apiconsumer.authenticate.Authenticate',
)

INSTALLED_APPS = ( 
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.messages',
    'api.courses',
    'api.apiconsumer',
    # Uncomment the next line to enable the admin:
    'django.contrib.admin',
    'api.static_content',
    'django_extensions', # used for debugging, remove if problematic
    'django.contrib.staticfiles',
    # Uncomment the next line to enable admin documentation:
    # 'django.contrib.admindocs',
)

And the django.wsgi file for what its worth. (Note, There is a lot of custom stuff in here.)

import os,sys

PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))

sys.path.append(PROJECT_PATH)


from sandbox_config import *

#Uncomment these two lines to use virtualenv
#activate_this = os.path.join(COURSESAPI_APP_ROOT, "ENV/bin/activate_this.py")
#execfile(activate_this, dict(__file__=activate_this))

sys.path.append(DEV_ROOT)
sys.path.append(COURSESAPI_APP_ROOT)

os.environ['DJANGO_SETTINGS_MODULE'] = 'settings'

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

Unfortunately, at this point I'm not really sure what to try. The error is extremely unhelpful.

  • Deleting the line in question caused the next custom middleware to break the program, which makes me think it may be a path issue.
  • Google doesn't seem to have much in the way of help.

Any ideas what the issue may be and what's worth trying?

Was it helpful?

Solution 2

Do the following files exist?

api/__init__.py
api/apiconsumer/__init__.py

If not, then api/apiconsumer/authenticate.py won't be found.

Also, try removing api from the prefix in the MIDDLEWARE_CLASSES and INSTALLED_APPS. So, you would have:

MIDDLEWARE_CLASSES = (
    ...
    'apiconsumer.authenticate.Authenticate',
)

INSTALLED_APPS = (
    ...
    'courses',
    'apiconsumer',
    'static_content',
    ...
)

OTHER TIPS

Where is this file (api/apiconsumer/authenticate.py)

No module named api.apiconsumer.authenticate

located (can you tell us your absolute path to this file?)?

Is it actually located at /home/davidxu/dev/Penn-Course-Review-api/api/apiconsumer/authenticate.py ?

If it's not at the correct filesystem path in relation to your wsgi process' PYTHONPATH, it wouldn't be able to load the code in that module.

Also, is your "api" app listed in your settings.py's INSTALLED_APPS ? Django will not know to search for "api.apiconsumer.authenticate" if you did not list "api" as an INSTALLED_APPS app.

EDITED

OP informed me that api is actually the name of his project and his app name is apiconsumer.

So my response resolved the problem:-

I suggest you keep your django project name clearly separated from app names. So use apiconsumer as the app name in your INSTALLED_APPS and use apiconsumer.authenticate.Authenticate in your MIDDLEWARE_CLASSES.

I was getting improperly configured while using imports in init.py.

The symptoms were: - runserver works as a charm - while deployed (apache/nginx+uwsgi) there are numerous errors with importing modules and finally ImproperlyConfigured is raised

The solution: Leave all init.py files empty.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top