Django/Apache/mod_wsgi不使用virtualenv的Python二进制文件

2024-04-24 02:57:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个virtualenv at/opt/webapps/ff/和它自己的Python安装。在我的Apache配置文件中,WSGIPythonHome被设置为/opt/webapps/ff(这肯定会在某种程度上被使用,因为如果我将它设置为稍微不同的现有目录并重新启动Apache,我会得到一个504)。但是,如果我在某个视图中打开Django调试页面,我会看到settings.PYTHON的BIN是/usr/bin,而不是/opt/webapps/ff/bin

如何让Apache/mod_wsgi使用虚拟环境的Python二进制文件?我以为设置WSGIPythonHome就是这样做的,但它似乎只影响使用哪个站点包dir,而不影响使用哪个二进制文件。谢谢。


Tags: 文件django目录视图binvirtualenvapache配置文件
2条回答

这些是我使用的说明,看起来效果不错。

http://code.google.com/p/modwsgi/wiki/VirtualEnvironments

Using 'site.addsitedir()' is a bit different to simply adding the directory to 'sys.path' as the function will open up any '.pth' files located in the directory and process them. This is necessary to ensure that any special directories related to Python eggs are automatically added to 'sys.path'.

Note that although virtualenv includes the script 'activate_this.py', which the virtualenv documentation claims should be invoked using 'execfile()' in the context of mod_wsgi, you may want to be cautious using it. This is because the script modifies 'sys.prefix' which may actually cause problems with the operation of mod_wsgi or Python modules already loaded into the Python interpreter, if the code is dependent on the value of 'sys.prefix' not changing. The WSGIPythonHome directive already described should instead be used if wanting to associate Python as a whole with the virtual environment.

Despite that, the 'activate_this.py' script is an attempt to resolve an issue with how 'site.addsitedir()' works. That is that any new directories which are added to 'sys.path' by 'site.addsitedir()' are actually appended to the end. The problem with this in the context of mod_wsgi is that if WSGIPythonHome was not used to associate mod_wsgi with a virgin baseline environment, then any packages/modules in the main Python installation will still take precedence over those in the virtual environment.

To work around this problem, what 'activate_this.py' does is invoke 'site.addsitedir()' but then also reorders 'sys.path' so any newly added directories are shifted to the front of 'sys.path'. This will then ensure that where there are different versions of packages in the virtual environment that they take precedence over those in the main Python installation.

As explained, because 'activate_this.py' is doing other things which may not be appropriate in the context of mod_wsgi, if unable to set WSGIPythonHome to point mod_wsgi at a virgin baseline environment, instead of just calling 'site.addsitedir()' you should use the code:

ALLDIRS = ['usr/local/pythonenv/PYLONS-1/lib/python2.5/site-packages']

import sys 
import site 

# Remember original sys.path.
prev_sys_path = list(sys.path) 

# Add each new site-packages directory.
for directory in ALLDIRS:
  site.addsitedir(directory)

# Reorder sys.path so new directories at the front.
new_sys_path = [] 
for item in list(sys.path): 
    if item not in prev_sys_path: 
        new_sys_path.append(item) 
        sys.path.remove(item) 
sys.path[:0] = new_sys_path 

If you still want to use the activation script from virtualenv, then use:

activate_this = '/usr/local/pythonenv/PYLONS-1/bin/activate_this.py' 
execfile(activate_this, dict(__file__=activate_this))

If the fact that 'sys.prefix' has been modified doesn't give an issue, then great. If you see subtle unexplained problems that may be linked to the change to 'sys.prefix', then use the more long handed approach above whereby 'site.addsitedir()' is used directly and 'sys.path' reorderd subsequently.

这里也有关于这个问题的讨论

http://groups.google.com/group/modwsgi/browse_thread/thread/466823f087070b5f?pli=1

我在Pylons应用程序中也遇到了同样的情况,结果改用了/usr/binbinaryplusvirtualenv site packages dir。

当然是同一个python版本。。。

如果使用的是virtualenv,则需要确保在WSGI脚本中激活它。

venv_path = "/opt/webapps/ff"
activate_this = os.path.join(venv_path, "bin/activate_this.py")
execfile(activate_this, dict(__file__=activate_this))

相关问题 更多 >