Skip to main content
 首页 » 编程设计

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

2024年10月17日30duanxz

我有一个带有 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 

请您参考如下方法:

在我的例子中,模块安装在用户环境中,而不是机器的环境中。 我只是运行:

sudo -H pip3.7 install mako 

-H 告诉 sudo 将模块安装在根机器目录与运行命令的用户目录中......

这是因为 Apache 无法访问/读取个人用户的文件。