Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django Apache and Virtualenv ImportError: No module named site

The error from apache after a 504 page

[info] mod_wsgi (pid=): Python home /var/venv/mybox.
[info] mod_wsgi (pid=): Initializing Python.
ImportError: No module named site

This is with a barely configured app.

<IfModule mod_wsgi.c>
WSGIDaemonProcess myapp python-home=/var/venv/mybox
WSGIProcessGroup myapp
WSGIScriptAlias / /var/www/html/web/myapp/wsgi.py
WSGISocketPrefix /var/run/wsgi

<Directory /var/www/html/web>
<Files wsgi.py>
Order deny,allow
Allow from all
</Files>
</Directory>
</IfModule>

Followed every post and tutorial I can. I am on CENTOS6 . using virutal env python 2.7 the default system env is 2.6

$ ldd /etc/httpd/modules/mod_wsgi.so
  linux-vdso.so.1 =>  (0x00007ffc06174000)

mywsgi.py

 import os,sys     
 from django.core.wsgi import get_wsgi_application     
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")
 sys.path.insert(0,'/var/www/html/web')
 activate_this = '/var/venv/mybox/bin/activate_this.py'
 execfile(activate_this, dict(__file__=activate_this))
 application = get_wsgi_application()

PYHTONHOME is not set

like image 235
Jabda Avatar asked Dec 06 '16 21:12

Jabda


4 Answers

I was on centos6.X with python 2.7, reinstall of mod_wsgi has fixed the issue for me.

yum remove mod_wsgi.x86_64
yum install mod_wsgi.x86_64
/etc/init.d/httpd restart 
like image 192
Naren Avatar answered Oct 16 '22 21:10

Naren


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!

like image 33
Lúcio Brígido Júnior Avatar answered Oct 16 '22 23:10

Lúcio Brígido Júnior


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.

like image 40
Graham Dumpleton Avatar answered Oct 16 '22 21:10

Graham Dumpleton


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=
like image 41
Ariel Livshits Avatar answered Oct 16 '22 22:10

Ariel Livshits