我有一个带有mod_wsgi的Apache服务器,运行Python 2.7脚本 . 该脚本使用通过pip安装的python Pillow模块 .
通常使用 python script.py
运行脚本可以正常工作,但是当从wsgi运行脚本时 - 为PIL抛出一个ImportError异常 .
这是 /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>
没有安装virtualenv,这台机器上只安装了一个Python .
我该怎么做才能使python找到已安装的模块?
我've seen solutions around the same ballpark that use mod_wsgi' s daemon mode手动定义python路径 . 有没有办法在嵌入模式下这样做?
编辑: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
普通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
2 回答
我重新编写了服务器配置,这次正确命名,使用virtualenv和守护进程模式到wsgi .
这是我最终得到的apache配置:
故事的道德启示? “时间很宝贵,明智地浪费”(不要半配置服务器配置)
我对此感到困惑,上面的讨论确实很有帮助 . 然而,我的解决方案只是在我的WSGI程序开头设置Python路径,例如: