Forums

WSGI.py doesn't run app expected

I'm using pythonanywhere to host as it seemed to be the most straight forward but having some issues Where am I meant to app.run()

When I try and import it into wsgi.py without using app.run() in my init.py file The wsgi.py file errors out with:

ImportError: cannot import name 'app' from 'init' (/home/g4t0/g4t0-flask/init.py)

But if I have app.run() it within the init.py file

it starts the dev webserver (rather than the prod wsgi one I want it to)

Where is the command app.run() meant to be? If anywhere? & if it's not meant to ran, then how do I configure wsgi.py to properly import the app?

from flask import Flask, render_template, session, url_for
from dotenv import load_dotenv
import os
load_dotenv()
secret_key = os.getenv('secret_key')

def init_app():

    app = Flask(__name__)
## generate a secret using secret module
    app.config['SECRET_KEY'] = f'{secret_key}'

# blueprint for auth routes in our app
    from auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

# blueprint for non-auth parts of app
    from main import main as main_blueprint
    app.register_blueprint(main_blueprint)

 # blueprint for admin
    from admin import admin as admin_blueprint
    app.register_blueprint(admin_blueprint)

    return app

if __name__ == "__main__":
    app = init_app()

The code in the if __name__ == "__main__": section is only run when you run that file directly and not when it's imported to be run by wsgi, so the variable app is never defined when you run that as a web app.

Hi Glenn, I get that, thanks...but when i specify app.run(), the development server runs - rather than the wsgi prod one that's expected...How do i get round this

Do not use app.run on PythonAnywhere. It will not work: https://help.pythonanywhere.com/pages/Flask/