Fixing “ImportError: No module named site” in Django/Apache/WSGI Setup on Ubuntu


5 views

After upgrading to Ubuntu 11.04, my Django application stopped working with Apache/mod_wsgi, flooding the error logs with thousands of:

ImportError: No module named site

What's particularly strange is that these errors appear even when no web requests are being made to the server.

The "No module named site" error typically occurs when Python can't locate its standard library. In a WSGI/Apache setup, this usually happens due to:

  • Incorrect Python path configuration
  • Virtual environment issues
  • WSGI/Python version mismatch
  • Environment variable conflicts

After extensive testing, here are the critical changes needed in the Apache configuration:

# Remove these problematic lines
# WSGIPythonHome /home/rory/tix/virtualenv2.5/lib/python2.5/
# SetEnv PYTHONPATH /home/rory/tix/virtualenv2.5/lib/python2.5/

# Use this instead for virtualenv
WSGIPythonHome /home/rory/tix/virtualenv2.5
WSGIPythonPath /home/rory/tix/virtualenv2.5/lib/python2.5/site-packages:/home/rory/tix/tix

The most reliable solution is to modify the WSGI script to properly activate the virtual environment:

import os
import sys

# Add virtualenv site-packages to path
venv_path = '/home/rory/tix/virtualenv2.5'
site_packages = os.path.join(venv_path, 'lib/python2.5/site-packages')

# Remove any existing Python paths to avoid conflicts
sys.path = 
# Add our virtualenv paths sys.path.insert(0, site_packages) sys.path.insert(0, os.path.join(venv_path, 'bin')) sys.path.insert(0, '/home/rory/tix/tix') # Set environment variables os.environ['PYTHON_EGG_CACHE'] = '/home/rory/tix/tmp/python-eggs' os.environ['DJANGO_SETTINGS_MODULE'] = 'tix.settings.base' # Now import Django and create WSGI application from django.core.handlers.wsgi import WSGIHandler application = WSGIHandler()

Ensure the Apache user has proper access to all files:

sudo chown -R www-data:www-data /home/rory/tix/tmp
sudo chmod -R 755 /home/rory/tix/virtualenv2.5

To verify your Python paths from within WSGI, add this temporary debugging code:

def application(environ, start_response):
    status = '200 OK'
    output = b'Python paths:\n\n' + '\n'.join(sys.path).encode('utf-8')
    
    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]
  1. Verify Python version matches between system and virtualenv
  2. Remove all PYTHONPATH environment variable overrides
  3. Use WSGIPythonPath instead of SetEnv for path configuration
  4. Ensure WSGIPythonHome points to virtualenv root, not lib directory
  5. Check file permissions for all directories in the path

When running a Django application through mod_wsgi on Ubuntu 11.04, Apache's error log gets flooded with repeated messages:

ImportError: No module named site

This occurs even when no web requests are being made, suggesting a fundamental Python environment configuration issue rather than application-specific problems.

The configuration shows several important details:

WSGIPythonHome /home/rory/tix/virtualenv2.5/lib/python2.5/
SetEnv PYTHONPATH /home/rory/tix/virtualenv2.5/lib/python2.5/
WSGIDaemonProcess tix python-path=/home/rory/tix/virtualenv2.5/lib/python2.5/site-packages

The "No module named site" error typically indicates Python cannot find its standard library. Common causes include:

  • Incorrect Python path configuration in mod_wsgi
  • Virtual environment not properly activated
  • Python installation corruption
  • Permission issues accessing Python directories

Here's a corrected Apache configuration:

WSGIPythonHome /home/rory/tix/virtualenv2.5
WSGIPythonPath /home/rory/tix/virtualenv2.5/lib/python2.5/site-packages:/home/rory/tix/virtualenv2.5/lib/python2.5

<VirtualHost 127.0.0.1:80>
    ServerName tix
    WSGIDaemonProcess tix user=tix_wsgi group=tix_wsgi processes=4 threads=1 \
        python-home=/home/rory/tix/virtualenv2.5 \
        python-path=/home/rory/tix/virtualenv2.5/lib/python2.5/site-packages:/home/rory/tix/tix
    WSGIProcessGroup tix
    WSGIScriptAlias / /home/rory/tix/tix/apache/loader.wsgi
</VirtualHost>

1. WSGIPythonHome should point to the virtualenv root, not the Python lib directory
2. python-home parameter in WSGIDaemonProcess should match WSGIPythonHome
3. Ensure all paths have proper permissions for the Apache user

Create a test WSGI file to verify the environment:

import sys
def application(environ, start_response):
    status = '200 OK'
    output = b'Python Path:\\n' + '\\n'.join(sys.path).encode('utf-8')
    response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]
  • Check Apache error logs with tail -f /var/log/apache2/error.log
  • Verify virtualenv integrity with virtualenv --relocatable /path/to/venv
  • Test Python environment directly with sudo -u www-data /path/to/venv/bin/python -c "import site; print(site)"

After Ubuntu upgrades, you might need to:

sudo apt-get install --reinstall libapache2-mod-wsgi
sudo a2dismod wsgi
sudo a2enmod wsgi
sudo service apache2 restart