Search code examples
python-3.xflaskconnexion

connexion: Failed to find Flask application or factory in module


I've started to develop a Flask based API using Flasks Blueprints. That worked like a charm. Then I started to refactor it, to use connexion and a swagger specification. After that, I'm not able to launch the application. I get following error message:

Use "FLASK_APP={module}:name to specify one.'.format(module=module.__name__)
flask.cli.NoAppException: Failed to find Flask application or factory in module "api.app". Use "FLASK_APP=api.app:name to specify one.

Versions used:

  • Python 3.7.6
  • connexion 2.7.0
  • Flask 1.1.2

My structure looks like:

api
 |-.flaskenv
 |- init.py
 |- app.py
 

.flaskenv

FLASK_APP=app.py
FLASK_ENV=development

_init_.py

# from flask import Blueprint
# bp = Blueprint("api", __name__)
# from api import endpoints  # , errors  # noqa F401, errors, tokens

app.py

# from flask import Flask
# from api import bp as api_bp
import connexion


# app = Flask(__name__)
# app.register_blueprint(api_bp, url_prefix="/api")
app = connexion.FlaskApp(__name__, specification_dir="./")
app.add_api("swagger.yml")

Launch (inside my venv):

flask run --no-debugger

If I uncomment the classic Flask coding (and comment on the connexion coding), everything worked, and the endpoints are accessible.

I checked also the environment variables that are available at the time app = connexion.FlaskApp(__name__, specification_dir="./") is called. There are the values from .flaskenv available.

From my understanding, connexion has Flask bundled, and when using the class FlaskApp, it's creating an app like the vanilla Flask framework.

My example is the same as in the connexion documentation.

I've no idea why vanilla Flask creates the application and connexion not ...

Thanks.

mybecks


Solution

  • Solution

    app.py must be extended by one line to expose a Flask class object: application = app.app

    app.py
    
    import connexion
    
    app = connexion.App("__name__", specification_dir="./")
    app.add_api("swagger.yml")
    application = app.app