简体   繁体   中英

Why `sys.path` differs when executed in mod_wsgi and normal interpreter mode?

Environment: MAC OS 10.10.4 python 2.7.12 with pyenv mod_wsgi 3.4 Apache/2.4.10 (Unix)

I have a wsgi.py that print the sys.path:

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

from app import app as application

and I confirmed that during installing mod_wsgi , it is configured with the correct python version ( /Users/forever/.pyenv/shims/python : installed by pyenv ):

checking for apxs2... no
checking for apxs... /usr/sbin/apxs
checking Apache version... 2.4.10
checking for python... /Users/forever/.pyenv/shims/python
configure: creating ./config.status
config.status: creating Makefile

then configure Apache with following content:

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

The Apache log shows the mod_wsgi information which has correct python version configured :

[Tue May 30 13:59:28.658723 2017] [mpm_prefork:notice] [pid 4843] AH00163: Apache/2.4.10 (Unix) PHP/5.5.24 mod_wsgi/3.4 Python/2.7.12 configured -- resuming normal operations

Then weird part is that the sys.path output in Apache log:

['/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']

which seems like the python path of system default python, not the python I installed by pyenv . Then I check what the sys.path is with the python interpreter installed by pyenv , it shows:

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']

, which is different.

I would like to know how can this happen? and how to correct the python path used by mod_wsgi?

WSGI has not been configured to use your virtualenv Python. See the documentation on WSGI and virtualenv ; you need to configure the daemon process group to use the right binary:

# add the python-home argument here
WSGIDaemonProcess test python-home=/Users/forever/.pyenv/shims user=forever group=admin threads=5

Otherwise, as the documentation states:

As only the single application is being run within the daemon process group, the WSGIApplicationGroup directive is also being used. When this is used with the %{GLOBAL} value, it forces the WSGI application to run in the main Python interpreter context of each process.

Bold emphasis mine.

WSGIPythonHome should only be used in embedded mode (no WSGIDaemonProcess and WSGIProcessGroup directives).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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