0

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?

4
  • Did this work locally, but not on the server? Commented Nov 8, 2012 at 2:09
  • Our development process is to develop on the server, so there is no way to distinguish. Commented Nov 8, 2012 at 2:10
  • Where is the wsgi file in relation to the api/ director? Commented Nov 8, 2012 at 2:12
  • wsgi is directly inside api (api/django.wsgi) Commented Nov 8, 2012 at 2:13

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

6 Comments

The absolute path is :/home/davidxu/dev/Penn-Course-Review-api/api/apiconsumer/authenticate.py
typo? authenicate.py? ---> authenticate.py
See my updated answer about api being listed in INSTALLED_APPS?
api is not actually an app-- it is the name of the Django project, which happens to be inside of our main repo. Given that, our apps are all referred to as api.X
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.
|
1

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

2 Comments

In regards to the first and second, yes-- the third does not exist at all. What makes you suspect it is needed?
Because PROJECT_PATH = os.path.realpath(os.path.dirname(__file__)) adds ../api/ as a root in the python path. So, by doing api.apiconsumer, python was looking for ../api/api/apiconsumer/
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.