简体   繁体   English

VS Code:如何调试使用 Connexion 的 Flask 应用程序?

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

I have faced a problem with starting a Flask app.我在启动 Flask 应用程序时遇到了问题。 I am trying to run the app in debug mode with Visual Studio Code but it doesn't run properly.我正在尝试使用 Visual Studio Code 在调试模式下运行该应用程序,但它无法正常运行。

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这是来自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?问题是,每次我在调试模式下运行它时,程序都不会执行我的代码,而是显示此错误: Error: module 'application_hook' has no attribute 'FlaskApp'为什么会发生? 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).要修复“错误:模块 'application_hook' 没有属性 'FlaskApp'” ,请通过将FLASK_APP设置为启动应用程序的主文件的名称(例如,app.py 或 main.py)来更新 launch.json。

In addition, since you are using connexion , launch.json needs to be updated as follows:另外,由于您使用的是connexion ,launch.json 需要更新如下:

  • the module should be changed from flask to connexion模块应该从flask改为connexion
  • args : args
    • delete --no-debugger删除--no-debugger
    • add the path to your specification file将路径添加到您的规范文件
    • add --port and the port number添加--port和端口号

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:在主模块中, app.run()应该放在__main__保护中:

Main module (app.py or main.py)主模块(app.py 或 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)

I don't know why!!!我不知道为什么!!! But, I'm working on a Flask/Connexion project.但是,我正在做一个 Flask/Connexion 项目。 Two months ago I've struggled trying to debug that on VSCode.两个月前,我一直在努力尝试在 VSCode 上调试它。 I gave up when it just worked as expected with zero pain on PyCharm.当它在 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:现在,也许是由于一些 Linux/VSCode?PyCharm 更新,相同的源代码不再使用 PyCharm 进行“调试”,并且正在使用最简单的启动器处理 VSCode:

"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.是的,我正在使用 venv。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM