WSGIPythonPath在我的virtualenv中应该指向哪里?

2024-05-21 13:56:58 发布

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

我在虚拟环境中的lib内有一个名为python2.7的文件夹。

在阅读了半打教程之后,我不知道该把WSGIPythonPath指向什么。我见过一些指向site-packages的列表,但有些是一个由冒号:分隔的列表。

Syntax error on line 1019 of /etc/httpd/conf/httpd.conf:
WSGIPythonPath cannot occur within <VirtualHost> section

WSGIPythonPath在我的virtualenv中应该指向哪里?


Tags: 文件夹列表onlibpackagesconf虚拟环境site
3条回答

您得到错误是因为WSGIPythonPath指令不能在VirtualHost上下文中使用。您必须在主Apache配置文件中声明它。如果仍要指向VirtualHost上下文中virtualenv中的目录,请改用WSGIDaemonProcess指令,它有一个python路径选项供您声明相关的python目录。

例如:虚拟主机配置文件应如下所示:

<VirtualHost *:80>
ServerName example.com
CustomLog logs/example.com-access_log common
ErrorLog logs/example.com-error_log

WSGIDaemonProcess example.com python-path=/virtualenvpathto/site-packages:/pathto/exampleprojecthome
WSGIProcessGroup example.com

...
</VirtualHost>

如果有多个python目录要添加到$python_PATH环境变量中,则使用完整的冒号,例如import example.foo可以正常工作。在上面的例子中,有两个目录,它们或多或少取决于您如何设置项目。

如果您在windows上,请使用分号;而不是完整的冒号。

我希望这能有帮助。

这是官方文件:https://docs.djangoproject.com/en/1.10/howto/deployment/wsgi/modwsgi/#using-a-virtualenv

Using a virtualenv¶

If you install your project’s Python dependencies inside a virtualenv, you’ll need to add the path to this virtualenv’s site-packages directory to your Python path as well. To do this, add an additional path to your WSGIPythonPath directive, with multiple paths separated by a colon (:) if using a UNIX-like system, or a semicolon (;) if using Windows. If any part of a directory path contains a space character, the complete argument string to WSGIPythonPath must be quoted:

> WSGIPythonPath
> /path/to/mysite.com:/path/to/your/venv/lib/python3.X/site-packages

Make sure you give the correct path to your virtualenv, and replace python3.X with the correct Python version (e.g. python3.4).

tl;dr:使用WSGIDaemonProcess python-home=…使用WSGIPythonPathWSGIDaemonProcess python-path=…的替代方案(使用-path而不是-home!)不再推荐。

新旧路

正如@kaykae所提到的,WSGIPythonPath不能在VirtualHost上下文中使用,但是WSGIDaemonProcess python-path=…是等价的。但是,尽管这仍然可以工作,但它不再是使用虚拟Python环境设置Apachemod_wsgi的推荐方法:

Note that prior practice was that these ways of setting the Python module search path [namely WSGIDaemonProcess …python-path=… and WSGIPythonPath] were used to specify the location of the Python virtual environment. Specifically, they were used to add the site-packages directory of the Python virtual environment. You should not do that.

The better way to specify the location of the Python virtual environment is using the python-home option of the WSGIDaemonProcess directive for daemon mode, or the WSGIPythonHome directive for embedded mode. These ways of specifying the Python virtual environment have been available since mod_wsgi 3.0 and Linux distributions have not shipped such an old version of mod_wsgi for quite some time. If you are using the older way, please update your configurations.

(Source: WSGI Docs: User Guides: Virtual Environments)

如何用新的方法

尝试在VirtualHost上下文中配置mod_wsgi这一事实表明您使用了“守护进程模式”配置版本。根据上面的引用,建议将您的virtualenv环境包含到Python路径中的方法是在您的VirtualHost部分中这样一个部分(尽管它也可以在外部定义,因为它可以与您选择的守护进程组的myapp1标识符一起引用):

<IfModule mod_wsgi.c>
  WSGIDaemonProcess myapp1 user=user1 group=group1 threads=5 
python-home=/path/to/project/venv
</IfModule>

注意/path/to/project/venvvirtualenv环境的基路径。它将是您调用virtualenv venv创建它的目录中的子目录venv

还要注意,您可以在Python路径中添加其他路径,使您的import语句适用于不通过PIP或类似方式管理的包。例如,可以添加python-path=/path/to/project。不要使用这种机制告诉wsgi关于整个virtualenv设置的信息,因为它们引入了python-home

相关问题 更多 >