Python 为什么'sys.path'在mod_wsgi和普通解释器模式下执行时不同?

Python 为什么'sys.path'在mod_wsgi和普通解释器模式下执行时不同?,python,apache,python-2.7,mod-wsgi,Python,Apache,Python 2.7,Mod Wsgi,环境: MAC OS 10.10.4 带pyenv的python2.7.12 mod_wsgi 3.4 Apache/2.4.10(Unix) 我有一个wsgi.py打印sys.path: import sys print sys.path sys.path.insert(0, '...') from app import app as application 我确认在安装mod_wsgi期间,它配置了正确的python版本(/Users/forever/.pyenv/shimmes/p

环境:

MAC OS 10.10.4
带pyenv的python2.7.12
mod_wsgi 3.4
Apache/2.4.10(Unix)

我有一个wsgi.py打印sys.path:

import sys
print sys.path
sys.path.insert(0, '...')

from app import app as application
我确认在安装
mod_wsgi
期间,它配置了正确的python版本(/Users/forever/.pyenv/shimmes/python:由
pyenv
安装):

然后使用以下内容配置Apache:

<VirtualHost *:5000>
    ServerName loyal.jms.tw

    # WSGIPythonHome /Users/forever/.pyenv/shims
    WSGIDaemonProcess test user=forever group=admin threads=5
    WSGIScriptAlias / /Users/forever/git/test/app/wsgi.py

    <Directory /Users/forever/git/test/app>
        WSGIProcessGroup test
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>
奇怪的是Apache日志中的
sys.path
输出:

['/usr/lib/python27.zip', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-darwin', '/usr/lib/python2.7/plat-mac', '/usr/lib/python2.7/plat-mac/lib-scriptpackages', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/Library/Python/2.7/site-packages']
这看起来像是系统默认python的python路径,而不是我通过
pyenv
安装的python。然后,我用
pyenv
安装的python解释器检查
sys.path
是什么,它显示:

Python 2.7.12 (default, May 26 2017, 21:30:36)
[GCC 4.2.1 Compatible Apple LLVM 7.0.0 (clang-700.0.72)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/Users/forever/.pyenv/versions/2.7.12/lib/python27.zip', '/Users/forever/.pyenv/versions/2.7.12/lib/python2.7', '/Users/forever/.pyenv/versions/2.7.12/lib/python2.7/plat-darwin', '/Users/forever/.pyenv/versions/2.7.12/lib/python2.7/plat-mac', '/Users/forever/.pyenv/versions/2.7.12/lib/python2.7/plat-mac/lib-scriptpackages', '/Users/forever/.pyenv/versions/2.7.12/lib/python2.7/lib-tk', '/Users/forever/.pyenv/versions/2.7.12/lib/python2.7/lib-old', '/Users/forever/.pyenv/versions/2.7.12/lib/python2.7/lib-dynload', '/Users/forever/.pyenv/versions/2.7.12/lib/python2.7/site-packages']
,这是不同的


我想知道这是怎么发生的?以及如何更正mod_wsgi使用的python路径

WSGI尚未配置为使用virtualenv Python。看,;您需要配置守护进程进程组以使用正确的二进制文件:

# add the python-home argument here
WSGIDaemonProcess test python-home=/Users/forever/.pyenv/shims user=forever group=admin threads=5
否则,如文件所述:

由于只有单个应用程序在守护进程进程组中运行,因此也使用了
WSGIApplicationGroup
指令当它与
%{GLOBAL}
值一起使用时,它会强制WSGI应用程序在每个进程的主Python解释器上下文中运行。

我的


WSGIPythonHome
应仅在嵌入式模式下使用(无
WSGIDaemonProcess
WSGIProcessGroup
指令)。

WSGI未配置为使用virtualenv Python。看,;您需要配置守护进程进程组以使用正确的二进制文件:

# add the python-home argument here
WSGIDaemonProcess test python-home=/Users/forever/.pyenv/shims user=forever group=admin threads=5
否则,如文件所述:

由于只有单个应用程序在守护进程进程组中运行,因此也使用了
WSGIApplicationGroup
指令当它与
%{GLOBAL}
值一起使用时,它会强制WSGI应用程序在每个进程的主Python解释器上下文中运行。

我的


WSGIPythonHome
只能在嵌入式模式下使用(没有
WSGIDaemonProcess
WSGIProcessGroup
指令)。

您还没有将WSGI配置为使用您的virtualenv,看看为什么要使用mod_WSGI 3.4,那个版本很古老。您正在尝试使用
Server.app
中的版本吗?建议您不要这样做。无论哪种方式,如果mod_wsgi是为不同的Python安装编译的,您都不能强制它使用基于另一个Python虚拟环境的Python虚拟环境。请参阅
pip安装
方法。另外,
pyenv
生成Python安装,默认情况下,这些安装已中断,无法与嵌入式系统一起使用。看看你还没有配置WSGI来使用你的virtualenv,看看你为什么要使用mod_WSGI 3.4,这个版本很古老。您正在尝试使用
Server.app
中的版本吗?建议您不要这样做。无论哪种方式,如果mod_wsgi是为不同的Python安装编译的,您都不能强制它使用基于另一个Python虚拟环境的Python虚拟环境。请参阅
pip安装
方法。另外,
pyenv
生成Python安装,默认情况下,这些安装已中断,无法与嵌入式系统一起使用。请参见添加
python home=/Users/forever/.pyenv/shim
并重新启动后,在Apache err中有许多行
ImportError:No module named site
log@LucasYang:听起来像是Python版本不兼容,请参阅@LucasYang:还有一些帖子是关于使用WSGI和virtualenv引发该异常的,我建议在谷歌上快速搜索,看看是否有匹配的。谢谢,我已经读过这篇文章,但我使用相同的python版本编译mod_wsgi并在Apache中配置虚拟主机。我不知道为什么它不起作用。这就是为什么我建议进一步谷歌搜索;可能是Python home设置错误?在添加
Python home=/Users/forever/.pyenv/shim
并重新启动后,Apache err中有许多行
ImportError:No module named site
log@LucasYang:听起来像是Python版本不兼容,请参阅@LucasYang:还有一些关于使用WSGI和virtualenv引发该异常的其他帖子,我建议进行快速谷歌搜索,看看是否有匹配的。谢谢,我已经阅读了这篇帖子,但我使用相同的python版本编译mod_WSGI并在Apache中配置虚拟主机。我不知道为什么它不起作用。这就是为什么我建议进一步谷歌搜索;可能是Python主页设置错误?
# add the python-home argument here
WSGIDaemonProcess test python-home=/Users/forever/.pyenv/shims user=forever group=admin threads=5