2012-03-30 5 views
2

Ich Einrichtung Django mit Apache, mod_wsgi, virtuelle envDjango, mod_wsgi und virtuelle env

Ich habe eine virtuelle env, die ich hier verwenden möchten: [! Missleading Name - lange Geschichte] /home/andy/Dev/python/Asynchron-mongo/

I heruntergeladen mod_wsgi und es mit virtual_env als root zusammengestellt

./configure --with-python =/home/andy/Dev/python/async-mongo/bin/python

ich als root lief:

make install

I Setup WSGIPythonHome & Pfad in http.conf

WSGIPythonHome /home/andy/dev/python/async-mongo/ 
WSGIPythonPath /home/andy/dev/python/async-mongo/lib/python2.6/site-packages 
LoadModule wsgi_module /usr/lib/apache2/modules/mod_wsgi.so 

Ich glaube, ich die Anweisungen auf http://code.google.com/p/modwsgi/wiki/VirtualEnvironments gefolgt

Wenn ich die 'Hallo Welt' app es laufen

def application(environ, start_response): 
    status = '200 OK' 
    output = 'Hello World!' 
    response_headers = [('Content-type', 'text/plain'), 
         ('Content-Length', str(len(output)))] 
    start_response(status, response_headers) 

    return [output] 

funktioniert, wenn ich versuchen, ein Modul zu importieren nicht:

import sys; raise Exception(sys.path) 
def application(environ, start_response): 
    status = '200 OK' 
    output = 'Hello World!' 
    response_headers = [('Content-type', 'text/plain'), 
         ('Content-Length', str(len(output)))] 
    start_response(status, response_headers) 
    print >> sys.stderr, 'sys.prefix = %s' % repr(sys.prefix) 
    print >> sys.stderr, 'sys.path = %s' % repr(sys.path) 
    return [output] 

Der Fehler, den ich in den Apache-Logs zu sehen ist :

[Fr Mar 30 15:09:53 2012] [notice] Apache/2.2.20 (Ubuntu) mod_wsgi/3.3 Python/2.6.7 konfiguriert - r esuming Normalbetrieb

........

Ausnahme: [ '/ home/andy/dev/Python/Asynchron-Mongo', ‚/ home/andy/dev/python/async-mongo/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg ', ' /home/andy/dev/python/async-mongo/lib/python2.6/site -Packages/pip-1.0.2-py2.6.egg ', ' /home/andy/dev/python/async-mongo/lib/python2.6/site-packages/txmongo-0.3-py2.6-linux -i686.egg ', ' /home/andy/dev/python/async-mongo/lib/python2.6 ', ' /home/andy/dev/python/async-mongo/lib/python2.6/plat -linux2 ', '/home/andy/dev/python/async-mongo/lib/python2.6/lib-tk', '/home/andy/dev/python/async-mongo/lib/python2.6/lib-old ', ' /home/andy/dev/python/async-mongo/lib/python2.6/lib-dynload ', ' /usr/lib/python2.6 ',' /usr/lib/python2.6/ plat-linux2' , '/usr/lib/python2.6/lib-tk', '/home/andy/dev/python/async-mongo/lib/python2.6/site-packages']

Ich vermute, dass irgendwo ich irgendwie immer noch auf die alte System-Level-Python verweisen, aber ich kann nicht verstehen, wo. Wie kann ich das beheben?

+4

Sicher versagt es, weil du bist explizit eine Ausnahme auslösen? –

+0

Was ist der Systempfad, den Sie in wsgi.py festgelegt haben? –

+0

@DanielRoseman: Sein Problem ist nicht die Ausnahme, es ist der sys.path, den er durch die Ausnahmemeldung untersucht. –

Antwort

3

Sie sind hier eine Ausnahme erhöhen:

import sys; raise Exception(sys.path) 
+0

Oh. Hoppla. Ich sehe, Daniel Roseman hat das bereits in Kommentaren gesagt. Entschuldigung für die Wiederholung. – alan

+0

Ich glaube, er löst die Ausnahme nur aus, um sys.path zur Laufzeit zu überprüfen. –

+0

oops - Ich habe das in die Runtime inspiziert, weil es nicht funktionierte und es komplett vergessen hat - Entschuldigung, ich bin ein Idiot. –

1

Der Platz zum Geigen mit sys.Pfad ist bei your_application.wsgi. Ich denke nicht, dass das Kompilieren von mod_wsgi, das auf ein virtualenv Python-Binary verweist, eine gute Idee ist, der ganze Sinn von virtualenv liegt in der Flexibilität (mehrere Versionen von django zum Beispiel auf demselben Rechner spielen).

Mein Nehmen auf django, Apache, wsgi und virtualenv ist eine my_application.wsgi-Datei wie folgt:

import os 
import sys 
import site 

# Backup sys.path 
prev_sys_path = list(sys.path) 
# Add virtual environment to site directories: 
site.addsitedir('/var/lib/python-environments/my_env/lib/python2.6/site-packages') 

# settings.py sitting at /path/to/apps/my_application 
os.environ['DJANGO_SETTINGS_MODULE'] = 'my_application.settings' 
sys.path.append('/path/to/apps') 

# start the trick 
sys.path.extend([ 
    '/var/lib/python-environments/my_env/lib/python2.6/site-packages', 
    '/var/lib/python-environments/my_env/lib/python2.6/site-packages/django/contrib/admindocs', 
]) 
# Reorder syspath 
new_sys_path = [p for p in sys.path if p not in prev_sys_path] 
for item in new_sys_path: 
    sys.path.remove(item) 
# Make sure virtual env is first 
sys.path[:0] = new_sys_path 

import django.core.handlers.wsgi 
application = django.core.handlers.wsgi.WSGIHandler() 
Verwandte Themen