我的 WSGI 应用程序无法加载为 Python 模块。我究竟做错了什么

My WSGI application cannot be loaded as Python module. What am I doing wrong

我一直在尝试在服务器上获取 运行 的 WebAPI。但是,每次我通过网络浏览器登录网站时,我都会收到此错误

Internal Server Error

The server encountered an internal error or misconfiguration and was unable to complete your request.

Please contact the server administrator, jordi.duch@gmail.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

More information about this error may be available in the server error log. Apache/2.2.22 (Ubuntu) Server at complex.ffn.ub.es Port 80

我检查了错误日志文件,发现了以下内容。

[Thu Jul 14 17:16:08 2016] [error] [client 161.116.80.82] mod_wsgi (pid=21450): Target WSGI script '/home/xarxes_ub/python_code/configure.wsgi' cannot be loaded as Python module.

[Thu Jul 14 17:16:08 2016] [error] [client 161.116.80.82] mod_wsgi (pid=21450): Exception occurred processing WSGI script '/home/xarxes_ub/python_code/configure.wsgi'.

[Thu Jul 14 17:16:08 2016] [error] [client 161.116.80.82] Traceback (most recent call last):

[Thu Jul 14 17:16:08 2016] [error] [client 161.116.80.82] File "/home/xarxes_ub/python_code/configure.wsgi", line 10, in

[Thu Jul 14 17:16:08 2016] [error] [client 161.116.80.82] from MenuUB2 import app as application

[Thu Jul 14 17:16:08 2016] [error] [client 161.116.80.82] ImportError: No module named MenuUB2

我根据本网站给出的说明配置了我的 .wsgi 文件 http://flask.pocoo.org/docs/0.11/deploying/mod_wsgi/

它特别提到 "Keep in mind that you will have to actually install your application into the virtualenv as well. Alternatively there is the option to just patch the path in the .wsgi file before the import: import sys sys.path.insert(0, '/path/to/the/application')" 我就是这样做的

这是 configure.wsgi 文件

import sys
sys.path.insert(0, '/home/xarxes_ub/python_code/MenuUB2.py')

from MenuUB2 import app as application

另外这里是我编辑的apache配置文件。我用 ###.

突出显示了我添加的行
<VirtualHost *:80>

DocumentRoot /var/www/web_del_grup/

################Added lines###############
    WSGIScriptAlias /submitFrame /home/xarxes_ub/python_code/configure.wsgi
WSGIDaemonProcess MenuUB2 user=www-data group=www-data threads=5  
################Added lines###############  

#####################Added Directory####################
<Directory /home/xarxes_ub/python_code>
        WSGIScriptReloading On
    WSGIProcessGroup MenuUB2
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
 </Directory>
#####################Added Directory####################

ErrorLog /var/log/apache2/error.log

# Options -Indexes +FollowSymLinks MultiViews
# RewriteEngine on
# RewriteCond %{HTTP_REFERER} !^$
# RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?complex.ffn.ub.edu [NC]
# RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?complex.ffn.ub.es [NC]
# RewriteRule ^/xarxesub/(.json)$ - [F,NC,L]

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel warn

    CustomLog /var/log/apache2/access.log combined

请告诉我需要做什么。如果我能让这个工作,我会很放心。 另请注意,我尝试将 "configure.wsgi" 文件更改为 return 静态文件并且它有效 示例在这里

def application(environ, start_response):
    status = '200 OK'
    output = 'Hello World!'

    response_headers = [('Content-type', 'text/plain'),
                        ('Content-Length', str(len(output)))]

   start_response(status, response_headers)

    return [output] 

好吧,原来我的 configure.wsgi 文件有误

我应该命名包含应用程序的文件夹而不是应用程序的绝对路径所以 /home/xarxes_ub/python_code 而不是 /home/xarxes_ub/python_code/MenuUB2.py

这是编辑后的 ​​configure.wsgi 文件

import sys
sys.path.insert(0, '/home/xarxes_ub/python_code')

from MenuUB2 import app as application