在 Windows 上使用 Apache/mod_wsgi 从 virtualenv 运行 Python [英] Running Python from a virtualenv with Apache/mod_wsgi, on Windows

查看:34
本文介绍了在 Windows 上使用 Apache/mod_wsgi 从 virtualenv 运行 Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置 WAMP 服务器.我已经让 Apache 正常工作,而且我已经顺利安装了 mod_wsgi.

I'm trying to set up WAMP server. I've got Apache working correctly, and I've installed mod_wsgi without a hitch.

问题是,我正在为我的项目使用虚拟环境(使用 virtualenv).很明显,mod_wsgi 在定位我的 Django 安装时遇到了问题.

Problem is, I'm using virtual environments (using virtualenv) for my projects. So obviously, mod_wsgi is having problems locating my installation of Django.

我试图了解如何让 mod_wsgi 与 virtualenvs 一起工作.文档似乎认为这是不可能的:

I'm trying to understand how I can get mod_wsgi to work well with the virtualenvs. The documentation seems to think this isn't possible:

请注意,WSGIPythonHome 指令只能在 UNIX 系统上使用,在 Windows 系统上不可用.这是因为在 Windows 系统上,Python DLL 的位置似乎决定了 Python 将在哪里查找 Python 库文件.目前还不知道如何在 Windows 上创建独立于主要 Python 安装的独特基线环境.

Note that the WSGIPythonHome directive can only be used on UNIX systems and is not available on Windows systems. This is because on Windows systems the location of the Python DLL seems to be what dictates where Python will look for the Python library files. It is not known at this point how one could create a distinct baseline environment independent of the main Python installation on Windows.

从这里:mod_wsgi + virtualenv 文档.

有没有人知道如何进行这项工作?

Does anyone have some idea on how to make this work?

推荐答案

您可以通过 Python 以编程方式激活环境,然后在导入其他任何内容之前将其添加到您的 .wsgi 文件中.

You can activate the environment programmatically from Python adding this to your .wsgi file before importing anything else.

来自 virtualenv 的文档:

有时你不能或不想使用由创建的 Python 解释器虚拟环境.例如,在一个mod_python 或 mod_wsgi 环境,只有一名口译员.

Sometimes you can't or don't want to use the Python interpreter created by the virtualenv. For instance, in a mod_python or mod_wsgi environment, there is only one interpreter.

幸运的是,这很容易.您必须使用要安装的自定义 Python 解释器图书馆.但是要使用库,你只需要确保路径是正确的.一个脚本可用于修正路径.您可以设置环境如:

Luckily, it's easy. You must use the custom Python interpreter to install libraries. But to use libraries, you just have to be sure the path is correct. A script is available to correct the path. You can setup the environment like:

activate_this = '/path/to/env/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

这会改变 sys.path 甚至改变 sys.prefix,也让你使用现有的解释器.项目在您的环境中将首先出现在 sys.path 上,在全局项之前.但是,这无法撤销其他环境的激活,或导入的模块.你例如,不应该尝试在网络之前激活环境要求;你应该激活一个尽可能早的环境,和在那个过程中不要再这样做了.

This will change sys.path and even change sys.prefix, but also allow you to use an existing interpreter. Items in your environment will show up first on sys.path, before global items. However, this cannot undo the activation of other environments, or modules that have been imported. You shouldn't try to, for instance, activate an environment before a web request; you should activate one environment as early as possible, and not do it again in that process.

这篇关于在 Windows 上使用 Apache/mod_wsgi 从 virtualenv 运行 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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