Django Apache and Virtualenv ImportError: No module named site

The documentation for using virtual environments with mod_wsgi can be found at:

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

Most important in your case is the section:

  • Virtual Environment and Python Version

In that section it states:

When using a Python virtual environment with mod_wsgi, it is very important that it has been created using the same Python installation that mod_wsgi was originally compiled for. It is not possible to use a Python virtual environment to force mod_wsgi to use a different Python version, or even a different Python installation.

You cannot for example force mod_wsgi to use a Python virtual environment created using Python 3.5 when mod_wsgi was originally compiled for Python 2.7. This is because the Python library for the Python installation it was originally compiled against is linked directly into the mod_wsgi module.

So most likely what is happening is that mod_wsgi is compiled for Python 2.6. You cannot in this case force it to use a Python virtual environment created from Python 2.7. When you do this, you will get the error you see about site module being missing.

You will need to uninstall that mod_wsgi from system packages and install mod_wsgi from source code, compiling it against Python 2.7. The easiest way to do this might be to use the pip install method as described in:

  • https://pypi.python.org/pypi/mod_wsgi

Run pip install to install it in your virtual environment and then follow instructions in section 'Connecting into Apache installation' about configuring Apache to use it.


this is taken fron the Documentation write this: WSGIScriptAlias / /path/to/mysite.com/mysite/wsgi.py WSGIPythonPath /path/to/mysite.com

<Directory /path/to/mysite.com/mysite>
<Files wsgi.py>
Require all granted
</Files>
</Directory>

and this is specifically for the virtual env, you need to write the path to the site packeges of your python virtual env:

WSGIPythonPath /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages

the problem may also be in the - PYTHONHOME

Change the location of the standard Python libraries. By default, the libraries are searched in prefix/lib/pythonversion and exec_prefix/lib/pythonversion, where prefix and exec_prefix are installation-dependent directories, both defaulting to /usr/local.

When PYTHONHOME is set to a single directory, its value replaces both prefix and exec_prefix. To specify different values for these, set PYTHONHOME to prefix:exec_prefix.

Try to clean up your PYTHONHOME:

user$ export PYTHONHOME=

Solved in CentOS 7 with Apache 2.4.6

My whole server uses Python 2.7, but I've already installed Python 3.6 and my virtualenv is using Python 3.6.

After configured djang.conf (/etc/httpd/conf.d/django.conf) with this code:

<VirtualHost *:80>

WSGIDaemonProcess myProj python-home=/home/user/django-site/env python-path=/home/user/django-site
WSGIProcessGroup myProj
WSGIScriptAlias /myProj /home/user/django-site/my-project/wsgi.py


Alias /static /home/user/django-site/static
<Directory /home/user/django-site/static>
    Require all granted
</Directory>

<Directory /home/user/django-site/my-project>
    <Files wsgi.py>
        Require all granted
    </Files>
</Directory>

</VirtualHost>

And restarted my apache

sudo systemctl restart httpd

I got this error a thousand lines (/var/log/httpd/error_log)

ImportError: No module named site
ImportError: No module named site
ImportError: No module named site
ImportError: No module named site

The solution

First:

sudo grep wsgi /var/log/httpd/error_log

I got this:

[mpm_prefork:notice] [pid 62324] AH00163: Apache/2.4.6 (CentOS) PHP/7.0.33 mod_wsgi/3.4 Python/2.7.5 configured -- resuming normal operations

Note the Python version (2.7.5). What I did to get mod_wsgi according to my Python 3.6 is using:

yum list *mod_wsgi*
Installed packages
mod_wsgi.x86_64                          3.4-18.el7                          @base
Disponible packages
python35u-mod_wsgi.x86_64                4.6.2-1.ius.centos7                 ius
python36u-mod_wsgi.x86_64                4.6.2-1.ius.centos7                 ius

and then I installed the package python36u-mod_wsgi.x86_64:

sudo yum install python36u-mod_wsgi.x86_64

Then I restarted Apache service:

sudo systemctl restart httpd

And got this new line from logs:

[Fri Mar 29 12:33:26.788716 2019] [mpm_prefork:notice] [pid 76317] AH00163: Apache/2.4.6 (CentOS) PHP/7.0.33 mod_wsgi/4.6.2 Python/3.6 configured -- resuming normal operations

And everything works! :-)

Hope it helps you. C ya!