Apache Error logs: "ImportError: No module named site"

2,116 views
Skip to first unread message

Jared Vacanti

unread,
Feb 9, 2015, 12:31:51 AM2/9/15
to mod...@googlegroups.com
I've been working on this same error for the last few days, and can't seem to find any way around it. I'm trying to use a virtual environment with my python install in order to move my Flask app into production. When I restart my webserver (sudo service apache2 restart), I don't get anything reaction from the server when I point my browser to www.example.com

I'm using Ubuntu14.04, Apache/2.4.7 (Ubuntu), Python 2.7.6, & mod_wsgi 4.4.8. This is a designated server, and when I run python manage.py runserver (to launch a development environment) I can access the page at www.example.com:5000 (served at 0:0:0:0:5000), so internally python is working OK.

I ran mod_wsgi-express start-server to test the wsgi installation and was outputted 

Server URL         : http://localhost:8000
Server Root        : /tmp/mod_wsgi-localhost:8000:1000
Server Conf        : /tmp/mod_wsgi-localhost:8000:1000/httpd.conf
Error Log File     : /tmp/mod_wsgi-localhost:8000:1000/error_log
Request Capacity   : 5 (1 process * 5 threads)
Request Timeout    : 60 (seconds)
Queue Backlog      : 100 (connections)
Queue Timeout      : 45 (seconds)
Server Capacity    : 20 (event/worker), 20 (prefork)
Server Backlog     : 500 (connections)
Locale Setting     : en_US.UTF-8


Which leads me to believe the installation is working find. Here are the contents of both my WSGI Configuration:

import sys, os
# activate_this = '/var/www/myApp/venv/bin/activate.py'
# execfile(activate_this, dict(__file__=activate_this))
ALLDIRS = ['/var/www/myApp/venv/bin/activate_this.py']
import site
# Remember original sys.path.
prev_sys_path = list(sys.path)
# Add each new site-packages directory.
for directory in ALLDIRS
  site
.addsitedir(directory)
# Reorder sys.path so new directories at the front.
new_sys_path = []
for item in list(sys.path):
   
if item not in prev_sys_path:
        new_sys_path
.append(item)
      sys
.path.remove(item)
sys
.path[:0] = new_sys_path
sys
.path.insert(0, '/var/www/MyApp'
os
.chdir("/var/www/MyApp")
from MyApp import app as application


and my Apache configuration: 

WSGIPythonHome /var/www/myApp/venv/bin/activate
<VirtualHost *:80>
       
ServerName example.com
                 ServerAdmin info@example.com      
                 
WSGIScriptAlias / /var/www/myApp/myApp.wsgi
<Directory /var/www/myApp/>
       
    WSGIProcessGroup myApp
           WSGIApplicationGroup %{GLOBAL
           
Order allow,deny
           
Allow from all
     
</Directory>
ErrorLog ${APACHE_LOG_DIR}/
error.log
LogLevel warn
</VirtualHost>


I'm at a loss. When I run sudo service apache2 restart, and check my logs in /var/log/apache2/error.log all I have is thousands of lines of "Import Error: no module named site", because it is outputting it about once every second.

Any help or a reference to this problem would really be appreciated, otherwise I'll make sure to try and update this if I figure it out in the meantime. Thanks.

Graham Dumpleton

unread,
Feb 9, 2015, 12:48:53 AM2/9/15
to mod...@googlegroups.com
On 09/02/2015, at 4:31 PM, Jared Vacanti <jaredv...@gmail.com> wrote:

I've been working on this same error for the last few days, and can't seem to find any way around it. I'm trying to use a virtual environment with my python install in order to move my Flask app into production. When I restart my webserver (sudo service apache2 restart), I don't get anything reaction from the server when I point my browser to www.example.com

I'm using Ubuntu14.04, Apache/2.4.7 (Ubuntu), Python 2.7.6, & mod_wsgi 4.4.8. This is a designated server, and when I run python manage.py runserver (to launch a development environment) I can access the page at www.example.com:5000 (served at 0:0:0:0:5000), so internally python is working OK.

I ran mod_wsgi-express start-server to test the wsgi installation and was outputted 

Server URL         : http://localhost:8000
Server Root        : /tmp/mod_wsgi-localhost:8000:1000
Server Conf        : /tmp/mod_wsgi-localhost:8000:1000/httpd.conf
Error Log File     : /tmp/mod_wsgi-localhost:8000:1000/error_log
Request Capacity   : 5 (1 process * 5 threads)
Request Timeout    : 60 (seconds)
Queue Backlog      : 100 (connections)
Queue Timeout      : 45 (seconds)
Server Capacity    : 20 (event/worker), 20 (prefork)
Server Backlog     : 500 (connections)
Locale Setting     : en_US.UTF-8



Running pip install mod_wsgi and mod_wsgi-express is doing something completely independent of your system Apache/mod_wsgi installation, which is likely to have been set up by using OS packages and not using pip.

How did you install mod_wsgi into your system Apache installation?

Do you have any idea what version of Python the version of mod_wsgi installed into your Apache installation is using?

Can you find where the mod_wsgi.so is in your main Apache installation and run:

    ldd mod_wsgi.so

on it from within the directory where it is located?

Which leads me to believe the installation is working find. Here are the contents of both my WSGI Configuration:

import sys, os
# activate_this = '/var/www/myApp/venv/bin/activate.py'
# execfile(activate_this, dict(__file__=activate_this))
ALLDIRS = ['/var/www/myApp/venv/bin/activate_this.py']
import site
# Remember original sys.path.
prev_sys_path = list(sys.path)
# Add each new site-packages directory.
for directory in ALLDIRS
  site
.addsitedir(directory)
# Reorder sys.path so new directories at the front.
new_sys_path = []
for item in list(sys.path):
   
if item not in prev_sys_path:
        new_sys_path
.append(item)
      sys
.path.remove(item)
sys
.path[:0] = new_sys_path
sys
.path.insert(0, '/var/www/MyApp'


None of this mucking around with paths should be necessary if you are using a reasonably modern version of mod_wsgi. There are better ways of doing it from the Apache configuration for mod_wsgi.

That said, most Linux distributions still ship hopelessly out of data versions of mod_wsgi. Ubuntu 14.04 still ships mod_wsgi 3.4 from memory. If you are lucky 3.5.

os.chdir("/var/www/MyApp")
from MyApp import app as application


and my Apache configuration: 

WSGIPythonHome /var/www/myApp/venv/bin/activate


This is wrong for a start, although mod_wsgi should really ignore it when set wrongly.

This is meant to be set to the value of sys.prefix your Python installation outputs when you import sys from Python interactive interpreter and print it.

For example on my MacOS X box I get. That just happens to the the default OS version of Python, so I wouldn't actually need to set it as would be used already.

>>> import sys
>>> print sys.prefix
/System/Library/Frameworks/Python.framework/Versions/2.7

<VirtualHost *:80>
       
ServerName example.com
                 ServerAdmin info@example.com      
                 
WSGIScriptAlias / /var/www/myApp/myApp.wsgi
<Directory /var/www/myApp/>
       
    WSGIProcessGroup myApp
           WSGIApplicationGroup %{GLOBAL
           
Order allow,deny
           
Allow from all
     
</Directory>
ErrorLog ${APACHE_LOG_DIR}/
error.log
LogLevel warn
</VirtualHost>



I'm at a loss. When I run sudo service apache2 restart, and check my logs in /var/log/apache2/error.log all I have is thousands of lines of "Import Error: no module named site", because it is outputting it about once every second.

Any help or a reference to this problem would really be appreciated, otherwise I'll make sure to try and update this if I figure it out in the meantime. Thanks.


Graham

Jared Vacanti

unread,
Feb 9, 2015, 5:39:42 PM2/9/15
to mod...@googlegroups.com
I installed mod_wsgi via apt-get install libapache2-mod-wsgi, and when that didn't work I built from source. I do think you're correct that it's running with an incorrect version. Here is my output from ldd mod_wsgi.so which is stored at /usr/lib/apache2/modules/

linux-vdso.so.1 =>  (0x00007fff881fe000)        
libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f0132324000)
libpython2
.7.so.1.0 => /usr/lib/x86_64-linux-gnu/libpython2.7.so.1.0 (0x00007f0131dbd000)
libc
.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f01319f6000)
/lib64/ld-linux-x86-64.so.2 (0x00007f0132776000)
libz
.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f01317dd000)
libdl
.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f01315d9000)
libutil
.so.1 => /lib/x86_64-linux-gnu/libutil.so.1 (0x00007f01313d5000)
libm
.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f01310cf000)


Running
>>> import sys
>>> print sys.prefix
/usr

Still only getting the one error: "ImportError: No module named site"

Graham Dumpleton

unread,
Feb 9, 2015, 9:55:27 PM2/9/15
to mod...@googlegroups.com
When you compiled from source code, did you install libapache2-mod-wsgi first?

When you compiled from source code, which Python installation did you compile it against?

Did you only have the system Python version and that was what was used, or do you have some second Python version installed that you tried to use?

One main reason this error comes up is that you compiled mof_wsgi from source code against a Python version installed under /usr/local but didn't take the extra steps required so that it could find Python in that non standard location.

Graham

--
You received this message because you are subscribed to the Google Groups "modwsgi" group.
To unsubscribe from this group and stop receiving emails from it, send an email to modwsgi+u...@googlegroups.com.
To post to this group, send email to mod...@googlegroups.com.
Visit this group at http://groups.google.com/group/modwsgi.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages