Python/Django/WSGI/Apache – “ImportError: No module named site”

apache-2.2djangomod-wsgipythonwsgi

I am trying to use a django application on my local ubuntu machine. However the site doesn't work and my /var/log/apache2/errors.log is filled with messages like this:

ImportError: No module named site

My /var/log/apache2/error.log (for today) looks like this:

$ cat error.log | uniq -c
      1 [Wed Jun 29 09:37:37 2011] [notice] Apache/2.2.17 (Ubuntu) mod_wsgi/3.3 Python/2.7.1+ configured -- resuming normal operations
  12966 ImportError: No module named site

That's the notice that it started up when I turned on my machine, followed by 12,966 lines all saying the no module named site message

note the lack of a datetime field. These errors are repeated even when not going to the website (i.e. even when not making web requests). When going to the website in a browser, it just hangs, as if waiting for a large download.

Settings

Apache modules

I am using a python 2.5 virtualenv with lots of packages (incl. django 1.1) installed with pip. I have mod_wsgi loaded:

$ ls -l /etc/apache2/mods-enabled/wsgi*
lrwxrwxrwx 1 root root 27 2010-10-04 16:50 /etc/apache2/mods-enabled/wsgi.conf -> ../mods-available/wsgi.conf
lrwxrwxrwx 1 root root 27 2010-10-04 16:50 /etc/apache2/mods-enabled/wsgi.load -> ../mods-available/wsgi.load

I use "tix" as a domain name that's set to localhost in /etc/hosts

$ grep tix /etc/hosts
127.0.0.1   tix

Apache config

Here is my apache configuration (You can see some attempts to make it work, commented lines etc.):

# mod-wsgi enabled virtual host
WSGISocketPrefix /home/rory/tix/tix_wsgi/tmp
WSGIPythonHome /home/rory/tix/virtualenv2.5/lib/python2.5/
UnSetEnv PYTHONSTARTUP
SetEnv PYTHONPATH /home/rory/tix/virtualenv2.5/lib/python2.5/
#WSGIPythonEggs /home/rory/svn/tix/tmp/python-eggs

<VirtualHost 127.0.0.1:80>
    ServerName tix

    Alias /media /home/rory/tix/tix/media
    Alias /selenium /home/rory/tix/tix/tests/selenium

    <Directory /home/rory/tix/tix/media>
        SetHandler None
        Order allow,deny
        Allow from all
    </Directory>

    WSGIDaemonProcess tix user=tix_wsgi group=tix_wsgi processes=4 threads=1           python-path=/home/rory/tix/virtualenv2.5/lib/python2.5/site-packages

    WSGIScriptAlias / /home/rory/tix/tix/apache/loader.wsgi
    WSGIProcessGroup tix

    CustomLog /var/log/apache2/tix_access.log combined
    ErrorLog /var/log/apache2/tix_error.log


<Location /server-status>
    SetHandler server-status
    Order Deny,Allow
    Deny from all
</Location>

  <IfModule rewrite_module>
      RewriteEngine On
      RewriteCond %{HTTP_HOST} ^media.tix$ [NC]
      RewriteRule .? http://tix/media%{REQUEST_URI} [R=301,L]
  </IfModule>
</VirtualHost>

wsgi loader

Here is my loader.wsgi:

I used to have import site in this file, which I thought might have caused the problem, but I removed it and the errors keep coming up.

# loader.wsgi - WSGI adapter for tix django project
# The python paste wrapper catches apache 500 errors (Internal Server Errors) and gives debug output
# See http://pythonpaste.org/modules/exceptions.html
import os
import sys

os.environ['DJANGO_SETTINGS_MODULE'] = 'tix.settings.base'

from paste.exceptions.errormiddleware import ErrorMiddleware

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

application = ErrorMiddleware(tixette, debug=True, error_email='operator@example.com', error_subject_prefix='Alert: wsgi loader python paste: ', error_log='/tix/1.0/logs/paste.log',  show_exceptions_in_wsgi_errors=False)

This configuration used to work fine on Ubuntu 10.10, but since I upgraded to Ubuntu 11.04, I get the errors above.

Best Answer

Your mod_wsgi was compiled for Python 2.7. You cannot then try and point it at a Python 2.5 virtual environment.

Also, the setting:

WSGIPythonHome /home/rory/tix/virtualenv2.5/lib/python2.5/

is pointing at the wrong thing even if it was a Python 2.7 virtual environment.

The settings:

UnSetEnv PYTHONSTARTUP
SetEnv PYTHONPATH /home/rory/tix/virtualenv2.5/lib/python2.5/

will not do anything either and don't know where you got the idea you could do that.

FWIW, the mod_wsgi documentation on virtual environments can be found at:

https://modwsgi.readthedocs.io/en/develop/user-guides/virtual-environments.html

This isn't going to help you though because you seem to have a more basic problem with your mod_wsgi and Python installations to begin with. The issue potentially being a variant of:

https://modwsgi.readthedocs.io/en/develop/user-guides/installation-issues.html#multiple-python-versions

Where did you get the mod_wsgi.so you are using?

Where is the Python 2.7 installed?

What other Python versions do you have installed and where?

Related Topic