Why do I get the error "A valid Flask application was not obtained from..." when I use the flask cli to run my app?
Asked Answered
F

2

6

I try to use the flask cli to start my application, i.e. flask run. I use the FLASK_APP environment variable to point to my application, i.e. export FLASK_APP=package_name.wsgi:app

In my wsgi.py file, I create the app with a factory function, i.e. app = create_app(config) and my create_app method looks like this:

def create_app(config_object=LocalConfig):
    app = connexion.App(config_object.API_NAME,
                    specification_dir=config_object.API_SWAGGER_DIR,
                    debug=config_object.DEBUG)
    app.app.config.from_object(config_object)
    app.app.json_encoder = JSONEncoder
    app.add_api(config_object.API_SWAGGER_YAML,
            strict_validation=config_object.API_SWAGGER_STRICT,
            validate_responses=config_object.API_SWAGGER_VALIDATE)
    app = register_extensions(app)
    app = register_blueprints(app)
    return app

However, the application doesn't start, I get the error:

A valid Flask application was not obtained from "package_name.wsgi:app".

Why is this?

I can start my app normally when I use gunicorn, i.e. gunicorn package_name.wsgi:app

Frowsy answered 24/9, 2018 at 16:30 Comment(0)
F
15

My create_app function didn't return an object of class flask.app.Flask but an object of class connexion.apps.flask_app.FlaskApp, because I am using the connexion framework.

In my wsgi.py file, I could simply set:

application = create_app(config)
app = application.app

I didn't even have to do export FLASK_APP=package_name.wsgi:app anymore, autodescovery worked if the flask run command was executed in the folder where the wsgi.py file is.

Frowsy answered 25/9, 2018 at 12:5 Comment(0)
L
0
application = create_app(config)
app = application.app
App = app.app

For me I just created another variable pointing to connexion.app.Flask type and I set the environ as below

export FLASK_APP= __main__:App
flask shell 
Limnetic answered 11/7, 2022 at 10:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.