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

标签 python apache python-2.7 pip mod-wsgi

我有一个带有 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 无法访问/读取个人用户的文件。

关于python - Apache + mod_wsgi - Python 不加载已安装的模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40391409/

相关文章:

c# - Pythonnet如何正确停止脚本执行

python - 使用 get_user_info(user_id) Discord Python

python - 使用 Jinja2 (Flask) 模板引擎参数化可重用 block

apache - xampp apache 端口更改/扫描

python - 检测计算机何时解锁 Windows

Python .split 索引错误

python - 如何对以下内容进行矢量化,追加可能是瓶颈

php - 本地主机在更新到 Maverick/Yosemite OS 后不执行 php 文件

python - 输入二维矩阵的每个元素的最短方法

java - 尝试在 Ubuntu 15.04 上的 Tomcat8 上部署 WAR 时出错