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>
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:
flask
toconnexion
args
:--no-debugger
--port
and the port numberlaunch.json
In the main module,
app.run()
should be placed within the__main__
guard:Main module (app.py or main.py)