Python 在 Apache2 上尝试使用 WSGI 部署 Flask 时如何解决导入错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3696606/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to solve import errors while trying to deploy Flask using WSGI on Apache2
提问by Shane Reustle
I am having an issue deploying a flask app on apache2 using wsgi. I have posted the error logs and config files below. I have tried moving things around, renaming them, etc, but all give me an internal server error. Not sure why I'm getting the import error. Any input / suggestions are appreciated, thanks!
我在使用 wsgi 在 apache2 上部署 Flask 应用程序时遇到问题。我已经在下面发布了错误日志和配置文件。我试过移动东西,重命名它们等,但都给我一个内部服务器错误。不知道为什么我会收到导入错误。任何输入/建议表示赞赏,谢谢!
Here is my apache error.log
这是我的 apache error.log
[Sun Sep 12 20:47:59 2010] [error] [client] mod_wsgi (pid=9753): Target WSGI script '/sites/flaskfirst/wsgi.py' cannot be loaded as Python module.
[Sun Sep 12 20:47:59 2010] [error] [client] mod_wsgi (pid=9753): Exception occurred processing WSGI script '/sites/flaskfirst/wsgi.py'.
[Sun Sep 12 20:47:59 2010] [error] [client] Traceback (most recent call last):
[Sun Sep 12 20:47:59 2010] [error] [client] File "/sites/flaskfirst/wsgi.py", line 1, in <module>
[Sun Sep 12 20:47:59 2010] [error] [client] from app import app as application
[Sun Sep 12 20:47:59 2010] [error] [client] ImportError: No module named app
wsgi.py
wsgi.py
# This is wsgi.py
from app import app as application
app.py
应用程序
# This is app.py
from flask import Flask, render_template
import settings
app = Flask(__name__)
app.debug = settings.DEBUG
from views.homepage import *
from views.events import *
from views.submit import *
from views.feed import *
if __name__ == "__main__":
app.run()
Here is the basics of the directory tree, to give you an idea.
这里是目录树的基础知识,给你一个想法。
/flaskfirst/
/static/
/templates/
/views/
__init__.py
app.py
wsgi.py
Here is the apache virtualhost file
这是 apache 虚拟主机文件
<VirtualHost *:80>
ServerAdmin [email protected]
ServerName crath.org
DocumentRoot /sites/flaskfirst
# WSGI Settings
WSGIScriptAlias / /sites/flaskfirst/wsgi.py
WSGIDaemonProcess flaskfirst user=sreustle group=general processes=1 threads=10
WSGIProcessGroup flaskfirst
# Static Directories
Alias /static /sites/flaskfirst/static/
<Location "/static">
SetHandler None
</Location>
</VirtualHost>
采纳答案by Shane Reustle
Thanks to zarfand damjanon irc.freenode.org at #pocoo, they were able to help me get this fixed. The problem was the PythonPath was not correct. We fixed this by using the following wsgi.py
由于zarf和damjanirc.freenode.org上在#pocoo,他们能够帮我修复此问题。问题是 PythonPath 不正确。我们使用以下 wsgi.py 解决了这个问题
import sys
sys.path.insert(0, "/sites/flaskfirst")
from app import app
application = app
回答by Willyfrog
I used your solution to get it working but it kept duplicating the path in sys.path (you can write it out to see if it happens to you) so I made a little modification:
我用你的解决方案让它工作,但它一直在复制 sys.path 中的路径(你可以写出来看看它是否发生在你身上)所以我做了一些修改:
import sys
flaskfirst = "/sites/flaskfirst"
if not flaskfirst in sys.path:
sys.path.insert(0, flaskfirst)
from app import app
application = app
That way it's only included once
这样它只包含一次

