Fork me on GitHub
Suzf  Blog

How-to resolve Target WSGI script xxx.wsgi cannot be loaded as Python module

License: Attribution-NonCommercial-ShareAlike 4.0 International

本文出自 Suzf Blog。 如未注明,均为 SUZF.NET 原创。

转载请注明:http://suzf.net/post/904

情景再现: 在我将 Flask应用程序部署到 Apache 的时候,我得到了一个  500 internal sever error 的错误。 查看错误日志得到下面信息

[Tue Jun 21 11:29:46 2016] [notice] Apache/2.2.15 (Unix) DAV/2 PHP/5.3.3 mod_ssl/2.2.15 OpenSSL/1.0.1e-fips mod_wsgi/3.2 Python/2.7.3 mod_perl/2.0.4 Perl/v5.10.1 configured -- resuming #
[Tue Jun 21 11:29:49 2016] [error] [client 172.16.7.158] mod_wsgi (pid=1334): Target WSGI script '/var/www/html/report/report.wsgi' cannot be loaded as Python module.
[Tue Jun 21 11:29:49 2016] [error] [client 172.16.7.158] mod_wsgi (pid=1334): Exception occurred processing WSGI script '/var/www/html/report/report.wsgi'.
[Tue Jun 21 11:29:49 2016] [error] [client 172.16.7.158] Traceback (most recent call last):
[Tue Jun 21 11:29:49 2016] [error] [client 172.16.7.158]   File "/var/www/html/report/report.wsgi", line 10, in <module>
[Tue Jun 21 11:29:49 2016] [error] [client 172.16.7.158]     from report import app as application
[Tue Jun 21 11:29:49 2016] [error] [client 172.16.7.158]   File "/var/www/html/report/report.py", line 11, in <module>
[Tue Jun 21 11:29:49 2016] [error] [client 172.16.7.158]     from flask import Flask
[Tue Jun 21 11:29:49 2016] [error] [client 172.16.7.158] ImportError: No module named falsk

Python 应用程序是 通过源码编译的,安装目录为 /usr/local/Python2.7.3 Flask 模块是存在的, 问题就在 为啥 找不到她呢???

#python -V
Python 2.7.3

#pip freeze | grep -i flask
Flask==0.10.1
Flask-Bootstrap==3.3.5.7
Flask-Moment==0.5.1
Flask-Script==2.0.5
Flask-SQLAlchemy==2.1
Flask-WTF==0.12

看起来应该是  apache/mod_wsgi 所使用的Python版本不同  或者是 找的 Python 路径不对 我们先来看看 mode_wsgi 用的是哪个版本的Python  https://code.google.com/archive/p/modwsgi/wikis/CheckingYourInstallation.wiki (See Python Installation In Use and Python Shared Library).

现在在用的是什么版本的Python

In [1]: import os,sys
 
In [2]: sys.path
Out[2]: 
['',
 '/usr/local/bin',
 '/usr/local/lib/python2.7/site-packages/setuptools-18.7.1-py2.7.egg',
 '/usr/local/lib/python27.zip',
 '/usr/local/lib/python2.7',
 '/usr/local/lib/python2.7/plat-linux2',
 '/usr/local/lib/python2.7/lib-tk',
 '/usr/local/lib/python2.7/lib-old',
 '/usr/local/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/site-packages',
 '/usr/local/lib/python2.7/site-packages/IPython/extensions',
 '/root/.ipython']

In [3]: print sys.version
2.7.3 (default, Jul 15 2015, 04:32:15) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]

In [4]: print sys.prefix
/usr/local

因此对于 采用非标准路径安装的情况下 需要在 Apache 网站配置文件中添加如下配置

WSGIPythonHome /usr/local

之后重新加载 Apache 配置文件 Python Shared Library

^_^[11:49:47][[email protected] report]#rpm -qa | grep mod_wsgi
mod_wsgi-3.2-7.el6.x86_64
@_@[12:47:55][[email protected] report]#rpm -ql mod_wsgi-3.2-7.el6.x86_64
/etc/httpd/conf.d/wsgi.conf
/usr/lib64/httpd/modules/mod_wsgi.so
/usr/share/doc/mod_wsgi-3.2
/usr/share/doc/mod_wsgi-3.2/LICENCE
/usr/share/doc/mod_wsgi-3.2/README
@_@[12:48:08][[email protected] report]#ldd /usr/lib64/httpd/modules/mod_wsgi.so
  linux-vdso.so.1 =>  (0x00007ffd4c1dc000)
  libpython2.7.so.1.0 => /usr/local/Python2.7.3/lib/libpython2.7.so.1.0 (0x00007efdd747b000)
  libpthread.so.0 => /lib64/libpthread.so.0 (0x00007efdd725d000)
  libdl.so.2 => /lib64/libdl.so.2 (0x00007efdd7059000)
  libutil.so.1 => /lib64/libutil.so.1 (0x00007efdd6e56000)
  libm.so.6 => /lib64/libm.so.6 (0x00007efdd6bd2000)
  libc.so.6 => /lib64/libc.so.6 (0x00007efdd683d000)
  /lib64/ld-linux-x86-64.so.2 (0x00007efdd7a91000)

相关文档:

modwsgi-troubleshooting

target-wsgi-script-cannot-be-loaded-as-python-module-flask

modwsgi-CheckingYourInstallation-wiki  

「一键投喂 软糖/蛋糕/布丁/牛奶/冰阔乐!」

Suzf Blog

(๑>ڡ<)☆ 谢谢 ~

使用微信扫描二维码完成支付