Alex Rivera | Logout

ImportError: No module named django.core.handlers.wsgi in install django mod_wsgi config on apache

Asked 2010-08-27T13:41:08.343
33

I tried to install django to work with apache and mod_wsgi but get this error:

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

I'v read that it may be user error...

On the console (ssh), with root access, I don't have any problems accessing django.core.handlers.wsgi , but when apache asks to access it, it can't

My django.wsgi:

import os
import sys

sys.path.append('my/rep/parents/of/my/project')
sys.path.append('/usr/lib/python2.4/site-packages/django')
os.environ['DJANGO_SETTINGS_MODULE'] = 'montest.settings'
os.environ['PYTHON_EGG_CACHE'] = '/tmp'


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

My vhost.conf:

Alias /media/ my/rep/parents/of/my/projet/montest/media/

<Directory my/rep/parents/of/my/projet/montest/media>
    Order deny,allow
    Allow from all
</Directory>

WSGIScriptAlias /montest my/rep/parents/of/my/projet/django.wsgi

<Directory my/rep/parents/of/my/projet>
    Order deny,allow
    Allow from all
</Directory>

EDIT :

Ok my result for ldd mod_wsgi.so

linux-gate.so.1 => (0x0013c000) 
libpython2.6.so.1.0 => /usr/lib/libpython2.6.so.1.0 (0x00663000) 
libpthread.so.0 => /lib/libpthread.so.0 (0x00bff000) 
libdl.so.2 => /lib/libdl.so.2 (0x0023b000) 
libutil.so.1 => /lib/libutil.so.1 (0x00420000) 
libm.so.6 => /lib/libm.so.6 (0x00110000) 
libc.so.6 => /lib/libc.so.6 (0x00240000) /lib/ld-linux.so.2 (0x0059f000) 

So i decide to test my mod_wsgi install with the test.wsgi

test.wsgi

def application(environ, start_response): 
    status = '200 OK'
    output = 'Hello world, I am a wsgi app!' 
    response_headers = [('Content-Type', 'text/plain'), ('Content-Length', str(len(output)))] 
    start_response(status, response_headers) 
    return [output]

my vhost.con

Edit
Report

2 Answers

2

I solved this issue by adding the parent directory that holds my django installation to sys.path in wsgi.py. Here are my settings, FWIW:

/home/banjer/myproject/wsgi.py:

import os, sys
sys.path.append('/home/banjer/django')
sys.path.append('/home/banjer') # this line solved it
sys.executable = '/usr/local/python-2.7.2/bin/python'
os.environ['DJANGO_SETTINGS_MODULE'] = 'myproject.settings'

import django.core.handlers.wsgi
application = django.core.handlers.wsgi.WSGIHandler()
answered 2012-01-13T14:40:01.380
1

Check your site-package file permissions. None of the above solutions worked for me until I fix file permissions. Here's what's in my ssl_error_log file:

mod_wsgi (pid=986, process='OSQA', application='xxxxxx.yyy.com|/forum'): Loading WSGI script '/data/http/osqa/osqa.wsgi'. 
mod_wsgi (pid=986): Target WSGI script '/data/http/osqa/osqa.wsgi' cannot be loaded as Python module.
mod_wsgi (pid=986): Exception occurred processing WSGI script '/data/http/osqa/osqa.wsgi'. Traceback (most recent call last):   File "/data/http/osqa/osqa.wsgi", line 14, in <module>
            import django.core.handlers.wsgi  ImportError: No module named django.core.handlers.wsgi

But I solved it on my server. If you can do this on the command line, then this solution is for you:

python
>>> import django.core.handlers.wsgi
>>>

What worked is that I chmod go+rx site-packages libpython* (this might be overkill, but it worked for me.)

I'm running as httpd as apache.user, and running python as root, could see the packages just fine, but my permissions were not setup correctly (to read by everyone), and that's why httpd could not read the packages.

answered 2013-07-21T11:43:05.313

Your Answer