How does django work with virtualenv?

烂漫一生 提交于 2019-12-03 08:48:05

Web servers typically have a way to configure their Python use. You can set environment variables, paths, and so on. Use these tools to point to the virtualenv.

For example, in my Apache httpd.conf:

WSGIDaemonProcess myapp processes=2 threads=12 python-path=/home/nedbat/webapps/myapp/server:/home/nedbat/webapps/myapp/lib/python2.7
WSGIProcessGroup myapp
WSGIRestrictEmbedded On
WSGILazyInitialization On
WSGIScriptAlias / /home/nedbat/webapps/myapp/myapp.wsgi

and I have an myapp.wsgi file:

import os
import site
import sys

VE = '/home/nedbat/webapps/myapp/ve'

site.addsitedir(VE + '/lib/python2.7/site-packages')

os.environ['DJANGO_SETTINGS_MODULE'] = 'prod_settings'
os.environ['USE_PYPY'] = 'y'
os.environ['TMPDIR'] = '/home/nedbat/webapps/myapp/tmp'

from django.core.handlers.wsgi import WSGIHandler

application = WSGIHandler()
chryss

If you wish to use a virtualenv with Django in a production or even staging environment, I'd expect you to activate the environment and, depending on whether you do it manually or use e.g. virtualenvwrapper, set the appropriate environment variables. I'm not sure what purpose it would have to install Django in a virtual environment and then not use it.

By the way, just as I look at this I see the related question Django and VirtualEnv Development/Deployment Best Practices show up in the sidebar. This seems to be rather relevant for your set-up.

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