Import errors with WSGI, Flask, and Apache

◇◆丶佛笑我妖孽 提交于 2021-01-27 17:52:46

问题


Totally new to python-based web apps, so I'm somewhat lost. Here is my apache error:

[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] mod_wsgi (pid=23704): Target WSGI script '/home/http/public/hello/hello.wsgi' cannot be loaded as Python module.
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] mod_wsgi (pid=23704): Exception occurred processing WSGI script '/home/http/public/hello/hello.wsgi'.
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] Traceback (most recent call last):
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45]   File "/home/http/public/hello/hello.wsgi", line 3, in <module>
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45]     from hello import app as application
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45]   File "/home/http/public/hello/hello.py", line 1, in <module>
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45]     from flask import Flask
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] ImportError: No module named 'flask'
[Wed May 08 22:41:47 2013] [error] [client 64.56.91.45] File does not exist: /home/http/public/favicon.ico

Apparently, it cannot find the flask module. I've looked this up, and it seems that most people get it to work by appending the project directory to the path, like so: (hello.wsgi)

import sys
sys.path.insert(0, "/home/http/public/hello")
from hello import app as application

And here is hello.py:

from flask import Flask
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run(host='0.0.0.0')

Works great when ran as python hello.py, however, throws a 500 error when browsing to host/hello. Here's the Apache configuration:

WSGIDaemonProcess hello user=http group=http threads=5
WSGIScriptAlias /hello "/home/http/public/hello/hello.wsgi"

<Directory /home/http/public/hello/>
    WSGIProcessGroup hello
    WSGIApplicationGroup %{GLOBAL}
    Order deny,allow
    Allow from all
</Directory>

I'm somewhat lost.


回答1:


The mod_wsgi module is built against a specific Python version and can only work with that version. You cannot force it to use a different version. Thus if you have multiple versions installed, you need to make sure that you are installing your packages and running your code in development against the same version.

You can determine which Python version/installation mod_wsgi is using by running the test:

  • http://code.google.com/p/modwsgi/wiki/CheckingYourInstallation#Python_Installation_In_Use


来源:https://stackoverflow.com/questions/16451509/import-errors-with-wsgi-flask-and-apache

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!