简体   繁体   English

Django,mod_wsgi和虚拟环境

[英]Django, mod_wsgi and virtual env

I'm setting up Django with apache, mod_wsgi, virtual env 我用apache,mod_wsgi,虚拟环境设置Django

I have a virtual env that I want to use here: [Missleading name - long story!] /home/andy/Dev/python/async-mongo/ 我有一个虚拟的环境 ,我想在这里使用:[Missleading name - long story!] / home / andy / Dev / python / async-mongo /

I downloaded mod_wsgi and compiled it with virtual_env as root 我下载了mod_wsgi并使用virtual_env以root身份编译它

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

I ran as root: 我以root身份运行:

make install make install

I setup WSGIPythonHome & Path in http.conf 我在http.conf中设置了WSGIPythonHome&Path

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

I think I followed the instructions at http://code.google.com/p/modwsgi/wiki/VirtualEnvironments 我想我遵循了http://code.google.com/p/modwsgi/wiki/VirtualEnvironments上的说明

When I run the 'Hello World' app it works 当我运行“Hello World”应用程序时,它可以运行

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]

When I try to import a module it fails : 当我尝试导入模块时,它失败了

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]

The error I see in the apache logs is: 我在apache日志中看到的错误是:

[Fri Mar 30 15:09:53 2012] [notice] Apache/2.2.20 (Ubuntu) mod_wsgi/3.3 Python/2.6.7 configured -- resuming normal operations [Fri Mar 30 15:09:53 2012] [notice] Apache / 2.2.20(Ubuntu)mod_wsgi / 3.3配置Python / 2.6.7 - 恢复正常操作

........ ........

Exception: ['/home/andy/dev/python/async-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'] 例外:['/ home / andy / dev / python / async-mongo','/ home / andy / dev / python / async-mongo / lib / python2.6 / site -packages / setupupols = .6c11-py2.6 .egg','/ home / and / dev / python / async-mongo / lib / python2.6 / site-package / patch1.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 /蟒蛇/异步 - 蒙戈/ lib目录/ python2.6的/站点包]

I am guessing that somewhere somehow I am still referencing the old system level python but I can not understand where. 我猜不到某种程度上我仍然引用旧的系统级python,但我无法理解在哪里。 How can I fix this? 我怎样才能解决这个问题?

你在这里提出一个例外:

import sys; raise Exception(sys.path)

The place to fiddle with sys.path is at your_application.wsgi. 摆弄sys.path的地方是your_application.wsgi。 I don't think compiling mod_wsgi pointing to a virtualenv python binary is a good idea, the whole point of virtualenv is flexibility (having several versions of django playing nice in the same machine, for example). 我不认为编译指向virtualenv python二进制文件的mod_wsgi是一个好主意,virtualenv的重点是灵活性(例如,在同一台机器上有几个版本的django很好玩)。

My take on django, apache, wsgi and virtualenv is a my_application.wsgi file like this: 我对django,apache,wsgi和virtualenv的看法是一个my_application.wsgi文件,如下所示:

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM