VS Code: How to debug Flask app that uses Connexion?

2k views Asked by At

I have faced a problem with starting a Flask app. I am trying to run the app in debug mode with Visual Studio Code but it doesn't run properly.

Here is the main module code:

import os
import sys
import logging
import argparse
import connexion
import flask
from cwsm import connexion_manager
import connector.config as lc
_CONFIG = None

path = os.path.abspath("./")

lc.initConfig(path + "/connector/config/Legic.ini")

app = connexion.FlaskApp(__name__, specification_dir=path + "/connector/config")
app.add_api("connectore.yaml")
app.run(host="0.0.0.0", port=8080,debug=True)

if __name__ == '__main__':
   main()

Here is configuration for debug from launch.json

{

        "name": "Python: Flask",
        "type": "python",
        "request": "launch",
        "module": "flask",
        "env": {
            "FLASK_APP": "application_hook:FlaskApp('dev')",
            "FLASK_ENV": "development",
            "FLASK_DEBUG": "0"
        },
        "args": [
            "run",
            "--no-debugger"
        ],
        "jinja": true
    }

The problem is, each time I run it in debug mode the program does not execute my code instead it shows this error: Error: module 'application_hook' has no attribute 'FlaskApp' Why does it happen? Thanks in advance

A litle bit more traceback:

(.venv) PS C:\Users\fele\Documents\Git>  cd 'c:\Users\fele\Documents\Git'; & 'c:\Users\fele\Documents\Git\.venv\Scripts\python.exe' 'c:\Users\fele\.vscode\extensions\ms-python.python-2020.9.112786\pythonFiles\lib\python\debugpy\launcher' '51724' '--' '-m' 'flask' 'run' '--no-debugger' 
 * Serving Flask app "application_hook:FlaskApp('dev')"
 * Environment: development
 * Debug mode: off
C:\Users\fele\Documents\Git/connector/config/Git.ini
C:\Users\fele\Documents\Git\.venv\lib\site-packages\connexion\apps\flask_app.py:96: Warning: Silently ignoring app.run() because the application is run from the flask command line executable.  Consider putting app.run() behind an if __name__ == "__main__" guard to silence this warning.
  self.app.run(self.host, port=self.port, debug=self.debug, **options)
Usage: python -m flask run [OPTIONS]

Error: module 'application_hook' has no attribute 'FlaskApp'
(.venv) PS C:\Users\fele\Documents\Git> 
2

There are 2 answers

1
Christopher Peisert On

To fix "Error: module 'application_hook' has no attribute 'FlaskApp'", update launch.json by setting FLASK_APP to the name of the main file that launches the app (for example, app.py or main.py).

In addition, since you are using connexion, launch.json needs to be updated as follows:

  • the module should be changed from flask to connexion
  • args:
    • delete --no-debugger
    • add the path to your specification file
    • add --port and the port number

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python: Connexion",
            "type": "python",
            "request": "launch",
            "module": "connexion",
            "cwd": "${workspaceFolder}",
            "env": {
                "FLASK_APP": "app.py",
                "FLASK_ENV": "development",
                "FLASK_DEBUG": "1"
            },
            "args": [
                "run",
                "./connector/config",
                "--port",
                "8080"
            ],
            "jinja": true
        }
    ]
}

In the main module, app.run() should be placed within the __main__ guard:

Main module (app.py or main.py)

path = os.path.abspath("./")

lc.initConfig(path + "/connector/config/Legic.ini")

app = connexion.FlaskApp(__name__, specification_dir=path + "/connector/config")
app.add_api("connectore.yaml")


if __name__ == '__main__':
   app.run(host="0.0.0.0", port=8080,debug=True)
0
Marcio On

I don't know why!!! But, I'm working on a Flask/Connexion project. Two months ago I've struggled trying to debug that on VSCode. I gave up when it just worked as expected with zero pain on PyCharm. Now, maybe due to some Linux/VSCode?PyCharm update, the same source code is no longer 'debugable' with PyCharm and is working on VSCode with the most simple launcher:

"configurations": [
    {
        "name": "Python: Debug",
        "type": "python",
        "request": "launch",
        "program": "app.py",
        "console": "integratedTerminal"
    }
]

Don't ask me why! It's the same source code! And yes, I'm using venv.