Forums

WSGI File settings for Flask App setup to run with "python manage.py runserver"

I have a server app that is designed to be kicked-off with "python manage.py runserver" from a console on local python instances. I can run it without errors from the bash console, but it starts/runs on http://127.0.0.1:5000

How do I setup the WSGI file properly so that it is listening on the internet at my pythonanywhere URL?

Please advise.

Thank you!

If it's a regular Flask app, you just need to set up the WSGI file so that it imports the instance of the Flask object as application. For example, if you had a Flask app that looked like this:

from flask import Flask

app = Flask(__name__)

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

...where the instance of the Flask object is called app, like it normally is, then your WSGI file would look like this:

import sys

# add your project directory to the sys.path
project_home = u'/home/DrAgus/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

# import flask app but need to call it "application" for WSGI to work
from flask_app import app as application  # noqa

...with appropriate adjustments of the project_home directory to make it match the directory where your code is, and to flask_app to make it match the module where the app variable is defined.

Thank you!

When using:

from flask_app import app as application

What is the proper way to import the app from one file to another on python anywhere?

When I use:

from . import app

It cannot seem to find app...

What is the file structure (ie. which file is where, and which file is running that code?)

The flask app and all of the py file modules are all in the same directory.

The init.py file is setup as the flask app (not sure if this is an issue).

Thanks again.

The __init__.py file being set up as the flask app sounds weird, but I don't know if it's the issue. The "." import form means that it's trying to import app from the same module as the file where the import is. Is there a file called app in the same module as the file where you're doing the import?

I'm getting an imported but unused error on the from flask_app import app as application line. shouldn't that be getting passed in somewhere?

No, it has to be like that in the wsgi file. Put # noqa at the end of the line