简体   繁体   English

Apache + mod_wsgi - Python 不加载已安装的模块

[英]Apache + mod_wsgi - Python doesn't load installed modules

I have an Apache server with mod_wsgi, running an Python 2.7 script.我有一个带有 mod_wsgi 的 Apache 服务器,运行 Python 2.7 脚本。 The script uses the python Pillow module, installed via pip.该脚本使用通过 pip 安装的 python Pillow 模块。

Running the script normally using python script.py works okay, but when running the script from wsgi - An ImportError exception is thrown for PIL.使用python script.py正常运行脚本可以正常工作,但是从 wsgi 运行脚本时 - 会为 PIL 引发 ImportError 异常。

This is the Apache configuration from /etc/apache2/sites-enabled/000-default.conf :这是来自/etc/apache2/sites-enabled/000-default.conf的 Apache 配置:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        WSGIScriptAlias /wsgi/ /home/nitay/Desktop/WebsitePath/Python/wsgi.py

        <Directory "/home/nitay/Desktop/WebsitePath/Python">
            Require all granted
        </Directory>
</VirtualHost>

There's no virtualenv installed, and there's just one Python installation on this machine.没有安装virtualenv,这台机器上只有一个Python安装。

What can I do to make python find its installed modules?我该怎么做才能让 python 找到它安装的模块?

I've seen solutions around the same ballpark that use mod_wsgi's daemon mode to manually define python path.我已经看到了使用 mod_wsgi 的守护进程模式手动定义 python 路径的同一个球场的解决方案。 Is there a way to do so in embedded mode?有没有办法在嵌入式模式下这样做?

EDIT: Apache error log:编辑:Apache 错误日志:

[Wed Nov 02 16:08:02.931400 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] mod_wsgi (pid=48202): Target WSGI script '/home/nitay/Desktop/WebsitePath/Python/wsgi.py' cannot be loaded as Python module., referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931475 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] mod_wsgi (pid=48202): Exception occurred processing WSGI script '/home/nitay/Desktop/WebsitePath/Python/wsgi.py'., referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931557 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] Traceback (most recent call last):, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931601 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223]   File "/home/nitay/Desktop/WebsitePath/Python/wsgi.py", line 9, in <module>, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931687 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223]     import sprites, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931705 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223]   File "/home/nitay/Desktop/WebsitePath/Python/sprites.py", line 1, in <module>, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931767 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223]     from PIL import Image, referer: http://192.168.1.247/index.html
[Wed Nov 02 16:08:02.931830 2016] [wsgi:error] [pid 48202:tid 140100207392512] [client 192.168.1.179:29223] ImportError: No module named PIL, referer: http://192.168.1.247/index.html

sys.path & version for the normal Python and WSGI:正常 Python 和 WSGI 的 sys.path 和版本:

Normal:
>>> sys.version
'2.7.11+ (default, Apr 17 2016, 14:00:29) \n[GCC 5.3.1 20160413]'
>>> sys.path
['', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/nitay/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/gtk-2.0']

WSGI:
>>> sys.version
2.7.11+ (default, Apr 17 2016, 14:00:29) [GCC 5.3.1 20160413] 
>>> sys.path
['/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib

in my case, the modules were installed in a user env, vs the machine's env.就我而言,模块安装在用户环境中,而不是机器的环境中。 i just run:我只是运行:

sudo -H pip3.7 install mako

The -H says to sudo to install the module in the root machine directory vs user directory who run the command ... -H表示sudo将模块安装在根机器目录与运行命令的用户目录中......

this is because Apache cannot access/read personal user's files.这是因为 Apache 无法访问/读取个人用户的文件。

I redid the server configuration, this time naming things properly, used virtualenv, and Daemon mode to wsgi.我重做了服务器配置,这次正确命名,使用virtualenv,守护进程模式为wsgi。

Here's the apache configuration I ended up with:这是我最终得到的 apache 配置:

<VirtualHost *:80>
        ServerAdmin webmaster@localhost
        DocumentRoot /var/www/

        ErrorLog ${APACHE_LOG_DIR}/error.log
        CustomLog ${APACHE_LOG_DIR}/access.log combined

        WSGIDaemonProcess sprites-toolbox python-path=/home/nitay/Desktop/SpritesToolbox/Python:/home/nitay/Desktop/SpritesToolbox/Python/sprite-toolbox-env/lib/python2.7/site-packages
        WSGIProcessGroup sprites-toolbox

        WSGIScriptAlias /wsgi/ /home/nitay/Desktop/SpritesToolbox/Python/wsgi.py


        <Directory "/home/nitay/Desktop/SpritesToolbox/Python">
            Require all granted
        </Directory>
</VirtualHost>

Moral of the story?故事的寓意? "Time is precious, waste it wisely" (Don't half-ass server configurations) “时间是宝贵的,明智地浪费它”(不要半途而废的服务器配置)

I got stuck with this and the discussion above really helped.我被困在这个问题上,上面的讨论真的很有帮助。 My solution however, was simply to set up the Python path at the beginning of my WSGI program, eg:然而,我的解决方案只是在我的 WSGI 程序的开头设置 Python 路径,例如:

def application(environ, start_response):
    import sys
    path = "/usr/local/lib64/python2.7/site-packages/"
    if path not in sys.path: sys.path.append(path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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