Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

python - How to solve import errors while trying to deploy Flask using WSGI on Apache2

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!

Here is my 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

# 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

<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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Thanks to zarf and damjan on 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

import sys
sys.path.insert(0, "/sites/flaskfirst")

from app import app
application = app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...