Python – How to get apache to use python 2 with mod_wsgi

apache-2.2djangomod-wsgipython

I have an Arch Linux server that used to use Python 2.7.3 just fine with Apache and mod_wsgi-3.4-2. I have both Python 2.7.3 and 3.3.0 installed on the server, and both executables are located in /usr/bin. Recently it has started to use Python 3. I need Apache to go back to using Python 2.7. The error_log looks something like this on a single request:

[Thu Dec 13 00:18:50 2012] [notice] Apache/2.2.23 (Unix) mod_wsgi/3.4 Python/3.3.0 mod_ssl/2.2.23 OpenSSL/1.0.1c DAV/2 PHP/5.4.9 configured -- resuming normal operations
[Thu Dec 13 00:18:53 2012] [error] 3.3.0 (default, Sep 29 2012, 16:08:02) 
[Thu Dec 13 00:18:53 2012] [error] [GCC 4.7.1 20120721 (prerelease)]
[Thu Dec 13 00:18:53 2012] [error] 
[Thu Dec 13 00:18:53 2012] [error] [client 192.168.0.7] mod_wsgi (pid=6156): Target WSGI script '/path/to/webapp/apache/wsgi.py' cannot be loaded as Python module.
[Thu Dec 13 00:18:53 2012] [error] [client 192.168.0.7] mod_wsgi (pid=6156): Exception occurred processing WSGI script '/path/to/webapp/apache/wsgi.py'.
[Thu Dec 13 00:18:53 2012] [error] [client 192.168.0.7] Traceback (most recent call last):
[Thu Dec 13 00:18:53 2012] [error] [client 192.168.0.7]   File "/path/to/webapp/apache/wsgi.py", line 4, in <module>
[Thu Dec 13 00:18:53 2012] [error] [client 192.168.0.7]     from django.core.handlers import wsgi
[Thu Dec 13 00:18:53 2012] [error] [client 192.168.0.7] ImportError: No module named 'django'

Here's my wsgi.py file:

import os
import sys
from django.core.handlers import wsgi

site_path = '/path/to/webapp/csis3150site'
if site_path not in sys.path:
  sys.path.append(site_path)

# Set DJANGO_SETTINGS_MODULE env variable
os.environ['DJANGO_SETTINGS_MODULE'] = 'csis3150site.settings'

# start wsgi
application = wsgi.WSGIHandler()

This is the mod_wsgi-related configuration from httpd.conf:

LoadModule wsgi_module modules/mod_wsgi.so
WSGIScriptAlias / /path/to/webapp/apache/wsgi.py 
WSGIPythonPath /path/to/webapp

Best Answer

I was able to fix this be uninstalling the mod_wsgi package and replaced it with the mod_wsgi2 package.

Related Topic