Django的/阿帕奇/ mod_wsgi的未使用的virtualenv的Python的二进制 [英] Django/Apache/mod_wsgi not using virtualenv's Python binary

查看:134
本文介绍了Django的/阿帕奇/ mod_wsgi的未使用的virtualenv的Python的二进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在/ opt / webapps /下FF /有自己的Python安装的virtualenv。我已经WSGIPythonHome设置到/ opt / web应用在我的Apache的配置文件/ FF(这肯定是越来越在一些能力,因为如果我把它设置为一个稍微不同的现有目录,并重新启动Apache,我得到一个504)。但是,如果我如断言假视图中的某处,弹出Django的调试页面,我看到settings.PYTHON_BIN是的/ usr / bin中而不是的/ opt / webapps /下FF /斌

我如何得到阿帕奇/ mod_wsgi的用我的虚拟环境的Python的二进制?我想设置WSGIPythonHome是做到这一点的方式,但它似乎只影响到哪些站点包目录时,不能使用哪个二进制文件。谢谢你。


解决方案

这是我使用的指令,这似乎运作良好。

<一个href=\"http://$c$c.google.com/p/modwsgi/wiki/VirtualEnvironments\">http://$c$c.google.com/p/modwsgi/wiki/VirtualEnvironments


  

使用'site.addsitedir()'是有点
  不同于简单的添加
  目录sys.path中'作为
  功能将打开任何.pth文件
  位于目录和过程
  他们。这是必要的,以确保
  与之相关的任何特殊目录
  Python的鸡蛋被自动添加到
  sys.path中。


  
  

请注意,尽管包含的virtualenv
  脚本'activate_this.py',这
  该文件的virtualenv索赔
  应该使用'的execfile()被调用
  在mod_wsgi的背景下​​,你可能
  想用它来保持谨慎。这是
  因为剧本修改
  SYS。preFIX实际上可能原因
  与操作方面的问题
  mod_wsgi的或Python模块已经
  加载到Python间preTER,如果
  在code是依赖于价值
  SYS。preFIX不改变。该
  WSGIPythonHome指令已经
  说明应改为若使用
  想Python作为一个整体关联
  与虚拟环境


  
  

尽管如此,'activate_this.py'
  脚本是解决一个尝试
  ')site.addsitedir('问题与如何
  作品。也就是说,任何新的
  其中加入到目录
  sys.path中'通过'site.addsitedir()'是
  实际上追加到末尾。该
  在的情况下的问题与此
  mod_wsgi的是,如果WSGIPythonHome是
  不习惯的mod_wsgi与关联
  处女基准环境,那么任何
  在主Python包/模块
  安装将仍然需要
  precedence对这些虚拟
  环境。


  
  

要解决此问题,有什么
  activate_this.py'确实是调用
  site.addsitedir(),但随后也
  重新排序的sys.path',所以任何新添加
  目录被转移到前
  的sys.path中。然后这将确保
  哪里有不同
  在虚拟包的版本
  他们采取precedence环境
  对这些在主体的Python
  安装。


  
  

正如所解释的,因为
  activate_this.py其他正在做
  东西可能无法在恰当
  的mod_wsgi的上下文中,如果无法
  设置WSGIPythonHome点的mod_wsgi
  在处女基准环境,
  而不是只打电话
  site.addsitedir()',你应该使用
  code:


  ALLDIRS = ['USR /本地/ pythonenv /挂架-1 / lib中/的python2.5 / site-packages中']进口SYS
导入站点#记得原来的sys.path。
prev_sys_path =列表(sys.path中)#将每一个新的site-packages目录。
在ALLDIRS目录:
  site.addsitedir(目录)#在前面重新排序的sys.path所以新的目录。
new_sys_path = []
在列表(sys.path的)项:
    如果不是在prev_sys_path项目:
        new_sys_path.append(项目)
        sys.path.remove(项目)
的sys.path [:0] = new_sys_path


  

如果你仍然想使用
  从virtualenv中的激活脚本,
  然后用:


  activate_this ='/usr/local/pythonenv/PYLONS-1/bin/activate_this.py
的execfile(activate_this,字典(__ __文件= activate_this))


  

如果一个事实,即SYS。preFIX已
  修改不给的问题,那么
  大。如果你看到细微的原因不明
  可以被链接到的问题
  更改为SYS。preFIX',然后使用
  更长远递给上述方法
  由此'site.addsitedir()'用于
  直接和'sys.path的reorderd
  随后


下面是关于这一问题的讨论,以及

<一个href=\"http://groups.google.com/group/modwsgi/browse_thread/thread/466823f087070b5f?pli=1\">http://groups.google.com/group/modwsgi/browse_thread/thread/466823f087070b5f?pli=1

I have a virtualenv at /opt/webapps/ff/ with its own Python installation. I have WSGIPythonHome set to /opt/webapps/ff in my Apache config file (and this is definitely getting used in some capacity, because if I set it to a slightly different existing directory and restart Apache I get a 504). But if I e.g. assert False in a view somewhere to bring up the Django debug page, I see that settings.PYTHON_BIN is /usr/bin rather than /opt/webapps/ff/bin.

How do I get Apache/mod_wsgi to use my virtual environment's Python binary? I thought setting WSGIPythonHome was the way to do this, but it only seems to affect which site-packages dir is used, not which binary is used. Thanks.

解决方案

These are the instructions I used which seem to be working well.

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.

Here is a discussion about this issue as well

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

这篇关于Django的/阿帕奇/ mod_wsgi的未使用的virtualenv的Python的二进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆