我有一个带有 mod_wsgi 的 Apache 服务器,运行 Python 2.7 脚本。
该脚本使用通过 pip 安装的 python Pillow 模块。
通常使用运行脚本python script.py
工作正常,但从 wsgi 运行脚本时 - PIL 会引发 ImportError 异常。
这是 Apache 配置/etc/apache2/sites-enabled/000-default.conf
:
<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 找到它已安装的模块?
我已经看到了使用 mod_wsgi 的相同解决方案守护进程模式 http://modwsgi.readthedocs.io/en/develop/user-guides/checking-your-installation.html?highlight=daemon#embedded-or-daemon-mode手动定义Python路径。有没有办法在嵌入模式下做到这一点?
编辑:
阿帕奇错误日志:
[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