简体   繁体   English

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

[英]Django/Apache/mod_wsgi not using virtualenv's Python binary

I have a virtualenv at /opt/webapps/ff/ with its own Python installation.我在 /opt/webapps/ff/ 有一个 virtualenv,它有自己的 Python 安装。 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).我在我的 Apache 配置文件中将 WSGIPythonHome 设置为 /opt/webapps/ff(这肯定会在某些容量中使用,因为如果我将其设置为稍微不同的现有目录并重新启动 Apache 我会得到 504)。 But if I eg 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 .但是,如果我在某个视图中assert False以调出 Django 调试页面,我会看到 settings.PYTHON_BIN 是/usr/bin而不是/opt/webapps/ff/bin

How do I get Apache/mod_wsgi to use my virtual environment's Python binary?如何让 Apache/mod_wsgi 使用我的虚拟环境的 Python 二进制文件? 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.我认为设置 WSGIPythonHome 是这样做的方法,但它似乎只影响使用哪个站点包目录,而不影响使用哪个二进制文件。 Thanks.谢谢。

These are the instructions I used which seem to be working well.这些是我使用的指令,似乎运行良好。

http://code.google.com/p/modwsgi/wiki/VirtualEnvironments 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.使用 'site.addsitedir()' 与简单地将目录添加到 'sys.path' 有点不同,因为 function 将打开目录中的任何 '.pth' 文件并处理它们。 This is necessary to ensure that any special directories related to Python eggs are automatically added to 'sys.path'.这是确保任何与 Python 鸡蛋相关的特殊目录自动添加到“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.请注意,尽管 virtualenv 包含脚本 'activate_this.py',virtualenv 文档声称应在 mod_wsgi 的上下文中使用 'execfile()' 调用该脚本,但您可能需要谨慎使用它。 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.这是因为脚本修改了“sys.prefix”,如果代码依赖于“sys.prefix”的值不变,这实际上可能会导致已经加载到 Python 解释器中的 mod_wsgi 或 Python 模块的操作出现问题。 The WSGIPythonHome directive already described should instead be used if wanting to associate Python as a whole with the virtual environment.如果想要将 Python 作为一个整体与虚拟环境相关联,则应该使用已经描述的 WSGIPythonHome 指令。

Despite that, the 'activate_this.py' script is an attempt to resolve an issue with how 'site.addsitedir()' works.尽管如此,“activate_this.py”脚本试图解决“site.addsitedir()”如何工作的问题。 That is that any new directories which are added to 'sys.path' by 'site.addsitedir()' are actually appended to the end.也就是说,由 'site.addsitedir()' 添加到 'sys.path' 的任何新目录实际上都附加到末尾。 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.在 mod_wsgi 上下文中的问题在于,如果不使用 WSGIPythonHome 将 mod_wsgi 与原始基线环境相关联,那么主 Python 安装中的任何包/模块仍将优先于虚拟环境中的那些。

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'.为了解决这个问题,'activate_this.py' 所做的是调用 'site.addsitedir()' 但随后也会重新排序 'sys.path' 以便任何新添加的目录都转移到 '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.这将确保在虚拟环境中存在不同版本的软件包时,它们优先于主 Python 安装中的软件包。

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:正如所解释的,因为 'activate_this.py' 正在做其他可能不适合 mod_wsgi 上下文的事情,如果无法将 WSGIPythonHome 设置为将 mod_wsgi 指向原始基线环境,而不是仅仅调用 'site.addsitedir()' 你应该使用代码:

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:如果您仍想使用 virtualenv 中的激活脚本,请使用:

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.如果 'sys.prefix' 已被修改的事实不会产生问题,那就太好了。 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.如果您看到可能与更改“sys.prefix”有关的细微无法解释的问题,请使用上述更长期的方法,即直接使用“site.addsitedir()”并随后重新排序“sys.path”。

Here is a discussion about this issue as well这里也是关于这个问题的讨论

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 had the same situation in a Pylons app and ended up using /usr/bin binary plus virtualenv site-packages dir instead.我在 Pylons 应用程序中遇到了同样的情况,最终改用/usr/bin二进制文件加上virtualenv site-packages 目录。

Of course it was the same python version...当然是同一个 python 版本...

If you're using a virtualenv, you need to be sure to activate it within the WSGI script.如果您使用的是 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))

I have encountered the same issue when installing modoboa (django based) in virtualenv.在 virtualenv 中安装 modoboa(基于 django)时遇到了同样的问题。

It took me a lot of time to find a clear answer, so I will post it here.我花了很多时间才找到一个明确的答案,所以我会在这里发布。

All I needed to do was adding two lines to the general apache conf file (/etc/httpd/conf/httpd.conf in CentOS):我需要做的就是在通用的 apache conf 文件(CentOS 中的 /etc/httpd/conf/httpd.conf)中添加两行:

WSGISocketPrefix /var/run/wsgi ## (location for the PID)
WSGIPythonHome /path/to/virtualenv 

And restart Apache并重启 Apache

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM