Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 371
Zuabi Avatar asked Sep 24 '18 16:09

Zuabi


1 Answers

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.

like image 130
Zuabi Avatar answered Oct 10 '22 21:10

Zuabi