目标WSGI脚本无法作为Python模块加载 - Flask

时间:2016-05-01 17:36:00

标签: python-2.7 flask wsgi ubuntu-server

我试图在浏览器中以适当的IP地址打开网站,我收到500内部服务器错误。我正在使用Digital Ocean和带有apache2的Ubuntu 14.04服务器。我收到500错误后运行

$ tail -f /var/log/apache2/error.log

然后我得到以下

    user@crimemap:~# tail -f /var/log/apache2/error.log
    [Sun May 01 11:32:48.811666 2016] [:error] [pid        14833:did 140337753298688] [remote    
    71.97.193.235:2804]  mod_wsgi (pid=14833): Target WSGI script '/var/www/crimemap/crimemap.wsgi' cannot 
    be loaded as Python module.
    [Sun May 01 11:32:48.811718 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804]
    mod_wsgi (pid=14833): Exception occurred processing WSGI script '/var/www/crimemap/crimemap.wsgi'.
    [Sun May 01 11:32:48.811749 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804] 
    Traceback (most recent call last):
    [Sun May 01 11:32:48.811788 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804]   
    File "/var/www/crimemap/crimemap.wsgi", line 3, in <module>
    [Sun May 01 11:32:48.811830 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804]     
    from crimemap import app as application
    [Sun May 01 11:32:48.811845 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804]   
    File   "/var/www/crimemap/crimemap.py", line 1, in <module>
    [Sun May 01 11:32:48.811900 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804]     
    from dbhelper import DBHelper
    [Sun May 01 11:32:48.811914 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804]   
    File "/var/www/crimemap/dbhelper.py", line 1, in <module>
    [Sun May 01 11:32:48.811932 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804]     
    import pymysql
    [Sun May 01 11:32:48.811958 2016] [:error] [pid 14833:tid 140337753298688] [remote 71.97.193.235:2804] 
    ImportError: No module named pymysql

然后我跑

user@crimemap:~# /var/www/crimemap/crimemap.wsgi

并获取

/var/www/crimemap/crimemap.wsgi: line 1: import: command not found
/var/www/crimemap/crimemap.wsgi: line 2: syntax error near unexpected token `0,'
/var/www/crimemap/crimemap.wsgi: line 2: `sys.path.insert(0, "/var/www/crimemap")'

我环顾四周,无法弄清楚语法错误?

这就是crimemap.wsgi的样子。

import sys
sys.path.insert(0, "/var/www/crimemap")
from crimemap import app as application

当我导入pymysql或其他.py文件时,我没有错误?

user@crimemap:~# python
Python 2.7.6 (default, Jun 22 2015, 17:58:13) 
[GCC 4.8.2] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pymysql
>>> 

我尝试在wsgi文件中添加一个shebang line和python路径,以确保它是可执行的。没有。确保默认文件不干扰,没有。我对此非常陌生,所以它可能是一个完全愚蠢的解决方案。如果是这样,我道歉,但我到处寻找。

1 个答案:

答案 0 :(得分:1)

看起来apache / mod_wsgi使用了“不同的”python。

您能否首先尝试找出mod_wsgi使用的是哪个python? https://code.google.com/archive/p/modwsgi/wikis/CheckingYourInstallation.wiki
(参见正在使用的Python安装 Python共享库)。