我正在使用apache,mod_wsgi,虚拟环境
设置Django我想在这里使用虚拟环境:[误导性名称 - 长篇故事!] /家庭/安迪/开发/蟒/异步-蒙戈/
我下载 mod_wsgi 并使用virtual_env作为root进行编译
./ configure --with-python = / home / andy / Dev / python / async-mongo / bin / python
我以root身份跑:
make install
我设置了WSGIPythonHome& 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
我想我按照http://code.google.com/p/modwsgi/wiki/VirtualEnvironments
上的说明操作了当我运行'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]
当我尝试导入模块时,失败:
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]
我在apache日志中看到的错误是:
[Fri Mar 30 15:09:53 2012] [notice] Apache / 2.2.20(Ubuntu)mod_wsgi / 3.3配置Python / 2.6.7 - 恢复正常操作
........
异常:['/ 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']
我猜不到某种程度上我还在引用旧的系统级python,但我无法理解在哪里。我该如何解决这个问题?
答案 0 :(得分:3)
你在这里提出例外:
import sys; raise Exception(sys.path)
答案 1 :(得分:1)
摆弄sys.path的地方是your_application.wsgi。我不认为编译指向virtualenv python二进制文件的mod_wsgi是一个好主意,virtualenv的重点是灵活性(例如,有几个版本的django在同一台机器中运行良好)。
我对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()