Ceasar
Ceasar

Reputation: 23093

Django middleware is ImproperlyConfigured?

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.

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

Upvotes: 0

Views: 4980

Answers (3)

Belda
Belda

Reputation: 609

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.

Upvotes: 0

Calvin Cheng
Calvin Cheng

Reputation: 36554

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.

Upvotes: 3

Arion
Arion

Reputation: 1880

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',
    ...
)

Upvotes: 1

Related Questions