Forums

Stuck trying to get hello world Flask app to run

I'm sure this is really simple, but I'm not sure what I'm doing wrong here.

Here's the error I'm seeing:

2017-02-03 19:02:08,263 :Error running WSGI application
2017-02-03 19:02:08,265 :ImportError: No module named 'flask_app'
2017-02-03 19:02:08,266 :  File "/var/www/tess_pythonanywhere_com_wsgi.py", line 119, in <module>
2017-02-03 19:02:08,266 :    from flask_app import app as application

And here's what my /var/www/tess_pythonanywhere_com_wsgi.py file looks like:

# +++++++++++ FLASK +++++++++++
# Flask works like any other WSGI-compatible framework, we just need
# to import the application.  Often Flask apps are called "app" so we
# may need to rename it during the import:
#
#
import sys
#
## The "/home/tess" below specifies your home
## directory -- the rest should be the directory you uploaded your Flask
## code to underneath the home directory.  So if you just ran
## "git clone git@github.com/myusername/myproject.git"
## ...or uploaded files to the directory "myproject", then you should
## specify "/home/tess/myproject"
path = '/home/tess/mysite'
if path not in sys.path:
    sys.path.append(path)

## After you uncomment the line below, the yellow triangle on the left
## side in our in-browser editor shows a warning saying:
##     'application' imported but unused.
## You can ignore this error. The line is necessary, and the variable
## is used externally.
from flask_app import app as application
#
# NB -- many Flask guides suggest you use a file called run.py; that's
# not necessary on PythonAnywhere.  And you should make sure your code
# does *not* invoke the flask development server with app.run(), as it
# will prevent your wsgi file from working.

And then the only file I have is ~/mysite/hello.py which is just:

from flask import Flask
app = Flask(__name__)

@app.route("/")
def hello():
    return "Hello World!"

if __name__ == "__main__":
    app.run()

Am I missing something important here? Is there configuration I should be doing elsewhere? Thanks!

You're importing from flask_app, but your module is called hello. Read the help here for a short intro about how Python finds things you're trying to import.

Thanks so much, Glenn, that did it!

I figured it was something like that, but I didn't connect that "flask_app" was supposed to be my code and not some built in name.

Change "from flask_app import app as application" in your wsgi.py file to "from hello import app as application"