Django, mod_wsgi and virtual env

為{幸葍}努か 提交于 2019-12-06 00:02:00

You are raising an exception here:

import sys; raise Exception(sys.path)

The place to fiddle with sys.path is at 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).

My take on django, apache, wsgi and virtualenv is a my_application.wsgi file like this:

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()
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!