views:

529

answers:

3

The issue I'm having is my wsgi file can't import the wsgi handlers properly.

/var/log/apache2/error.log reports:

ImportError: No module named django.core.handlers.wsgi

Googling this brings up a couple results, mostly dealing with permissions errors because www-data can't read certain files and/or the pythonpath is not correct. Some of the solutions are vague or just don't work in my circumstance.

Background information..

My /usr/lib directory..

/usr/lib/python2.4
/usr/lib/python2.5
/usr/lib/python2.6
/usr/lib/python-django

The default python version is 2.5.2. If I open the interpreter as a regular user I can import django.core.handlers.wsgi with no issues.

If I switch to www-data the python version is the same, and I can import the django.core.handlers.wsgi module no problem.

In my bashrc, I set my PYTHONPATH to my home directory which contains all my django sites...

export PYTHONPATH=/home/meder/django-sites/:$PYTHONPATH

So the directory structure is:

django-sites/
   test

test is the directory created by django-admin createproject.

My virtualhost:

<VirtualHost *:80>
    ServerName beta.blah.com
    WSGIScriptAlias / /home/meder/django-sites/test/apache/django.wsgi
    Alias /media /home/meder/django-sites/test/media/
</VirtualHost>

The /home/meder/django-sites/test/apache/django.wsgi file itself:

import os, sys

sys.path.append('/usr/local/django')
sys.path.append('/home/meder/django-sites')
sys.path.append('/home/meder/django-sites/test')
os.environ['DJANGO_SETTINGS_MODULE'] = 'test.settings'

import django.core.handlers.wsgi

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

Finally, my OS is Debian Lenny and I grabbed django 1.1.1 from backports. Hope that's enough information.

Update #1 - per the first reply here's the result of ldd /usr/lib/apache2/modules/mod_wsgi.so:

meder@site:/usr/lib/apache2/modules$ ldd mod_wsgi.so
    libpython2.5.so.1.0 => /usr/lib/libpython2.5.so.1.0 (0xb7d99000)
    libpthread.so.0 => /lib/libpthread.so.0 (0xb7d81000)
    libdl.so.2 => /lib/libdl.so.2 (0xb7d7c000)
    libutil.so.1 => /lib/libutil.so.1 (0xb7d78000)
    libm.so.6 => /lib/libm.so.6 (0xb7d52000)
    libc.so.6 => /lib/libc.so.6 (0xb7c14000)
    /lib/ld-linux.so.2 (0xb7efd000)

So it is compiled against python 2.5 and not 2.4.

A: 

Sounds like you mod_wsgi is not compiled against Python 2.5 and instead compiled against Python 2.4 or 2.6. Run:

ldd mod_wsgi.so

on the mod_wsgi.so file where it is installed to work out what it is using.

If it is different, you will need to recompile mod_wsgi from source code such that it uses the version you want to use.

Graham Dumpleton
I updated OP w/ results, it seems like it's compiled for 2.5.
meder
A: 

Since I'm on Debian it appears that django is in /usr/lib/pymodules/python2.5 and not /usr/lib/python2.5/site-packages.

I added

sys.path.append('/usr/lib/pymodules/python2.5') 

to the top of my wsgi file and that did it, although I feel as though I should be fixing this in a more proper manner.

meder
A: 

I don't think your problem lies with the sys.path. I've always used Mod_WSGI with Django using the Daemonized process, like so,

# Note these 2 lines
WSGIDaemonProcess site-1 user=user-1 group=user-1 threads=25
WSGIProcessGroup site-1

Alias /media/ /usr/local/django/mysite/media/

<Directory /usr/local/django/mysite/media>
Order deny,allow
Allow from all
</Directory>

WSGIScriptAlias / /usr/local/django/mysite/apache/django.wsgi

<Directory /usr/local/django/mysite/apache>
Order deny,allow
Allow from all

If you note the first 2 lines - you can specify the group and the user which will be running this. In your case, you mention that www-data can import the django module but it doesn't work when Apache deploys it - perhaps the process is being run by nobody, or some other user/group that does not have privileges to import this module. Adding the DaemonProcess and Group lines should solve your problem.

HTH.

[1] For reference - here's the Django Mod_WSGI doc - http://code.google.com/p/modwsgi/wiki/IntegrationWithDjango

viksit