【发布时间】:2026-02-10 07:10:02
【问题描述】:
我有一个带有 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 找到它安装的模块?
我已经看到使用 mod_wsgi 的 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
【问题讨论】:
-
当您正常运行并从 wsgi 运行时,我会匹配
sys.version和sys.path。 -
显示完整的错误消息和来自 Apache 错误日志的回溯。
-
还要解释一下为什么要使用嵌入模式?无论如何,使用守护程序模式是首选设置。
-
我想使用嵌入式模式,因为 mod_wsgi 可以工作,而设置 mod_wsgi 的过程并不顺利 - 很多事情都在途中中断。我想专注于这个项目的软件方面,但如果需要 - 守护进程模式它。为什么它是首选设置?
-
看来 mod_wsgi 以 su 运行 python,当以 su 运行 python 时,路径不包含任何站点包文件夹。
标签: python apache python-2.7 pip mod-wsgi