Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_wsgi python can't import from standard library

I have created two python environments with virtualenv: /usr/local/pythonenv/BASELINE and /usr/local/pythonenv/django1. Both were created with --no-site-packages. I installed django to the django1 environment with easy_install.

My wsgi.conf file has this line to set the Python interpreter:

WSGIPythonHome /usr/local/pythonenv/BASELINE

My django.wsgi file starts like:

import site
site.addsitedir('/usr/local/pythonenv/django1/lib/python2.7/site-packages')
import os
import sys

But when I try to visit my site, I get a 500 Error, and httpd/error_log contains:

[error] Traceback (most recent call last):
[error]   File "/service/usr/local/django_apps/apache/django.wsgi", line 1, in ?
[error]     import site
[error] ImportError: No module named site

I am lost as to why the Python interpreter can't import its own standard library. I'm not even sure how to check if httpd is even using the interpreter in /usr/local/pythonenv/BASELINE, so that would be a good start.

Edit: Unrelated but I was quite torn on whether I should post this here or to ServerFault. Advice on that front appreciated.

Edit: So I was able to get some debug information thanks to http://code.google.com/p/modwsgi/wiki/DebuggingTechniques. I changed my django.wsgi script to contain

import sys
def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'
    print >> environ['wsgi.errors'], sys.path
    print >> environ['wsgi.errors'], sys.prefix
    print >> environ['wsgi.errors'], sys.executable

    response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

This put the Python interpreter information in /var/log/httpd/error_log. The error output:

[error] ['/usr/local/pythonenv/BASELINE/lib64/python24.zip',     '/usr/local/pythonenv/BASELINE/lib64/python2.4/', '/usr/local/pythonenv/BASELINE/lib64/python2.4/plat-linux2', '/usr/local/pythonenv/BASELINE/lib64/python2.4/lib-tk', '/usr/local/pythonenv/BASELINE/lib64/python2.4/lib-dynload']
[error] /usr/local/pythonenv/BASELINE
[error] /usr/bin/python

So sys.path and sys.executable point to my problem. For some reason sys.path is using some lib files that do not exist (BASELINE does not even contain a lib64 directory, and it was created with Python2.7), and sys.executable shows that mod_wsgi is still running the default /usr/bin/python interpreter, not the interpreter in /usr/local/pythonenv/BASELINE/bin.

Not sure why this is the case, but at least I know a bit more now.

EDIT: This is solved, but Django still lists "Python Executable: /usr/bin/python" even though it should be (and as far as I can tell, it is, since Python Version: 2.7.2) using /usr/local/bin/python. Is this normal?

like image 263
sans Avatar asked Oct 13 '11 22:10

sans


1 Answers

Your mod_wsgi is likely not compiled against Python 2.7. You cannot use a virtualenv for one Python version with mod_wsgi compiled against a different Python version.

Read from:

http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Shared_Library

and do checks in that document.

like image 158
Graham Dumpleton Avatar answered Oct 29 '22 20:10

Graham Dumpleton