VS Code:如何调试使用 Connexion 的 Flask 应用程序? [英] VS Code: How to debug Flask app that uses Connexion?

查看:38
本文介绍了VS Code:如何调试使用 Connexion 的 Flask 应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在启动 Flask 应用程序时遇到了问题.我正在尝试使用 Visual Studio Code 在调试模式下运行该应用程序,但它无法正常运行.

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.

以下是主要模块代码:

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()

这里是 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
    }

问题是,每次我在调试模式下运行它时,程序都不会执行我的代码,而是显示以下错误:错误:模块'application_hook'没有属性'FlaskApp'为什么会发生?提前致谢

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

多一点追溯:

(.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> 

推荐答案

要修复错误:模块 'application_hook' 没有属性 'FlaskApp'",通过设置 FLASK_APP 到启动应用程序的主文件的名称(例如,app.py 或 main.py).

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).

另外,由于您使用的是connexion,launch.json 需要更新如下:

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

  • 模块应该从flask改为connexion
  • 参数:
    • 删除--no-debugger
    • 将路径添加到您的规范文件
    • 添加--port和端口号
    • 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
      {
          "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
              }
          ]
      }
      

      在主模块中,app.run() 应该放在 __main__ 守卫中:

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

      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)
      

      这篇关于VS Code:如何调试使用 Connexion 的 Flask 应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆