在Windows上使用Apache/mod_wsgi从virtualenv运行Python

2024-05-29 00:25:23 发布

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

我正在尝试设置WAMP服务器。我已经让Apache正常工作了,而且我已经毫无障碍地安装了mod_wsgi。

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

我在试着理解如何让mod_wsgi和virtualenvs一起工作。文档似乎认为这是不可能的:

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 docs

有人知道怎么做吗?


Tags: ofthe服务器modwsgivirtualenvison
1条回答
网友
1楼 · 发布于 2024-05-29 00:25:23

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

来自virtualenv's docs

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.

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))

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.

相关问题 更多 >

    热门问题