1

我在启动 Flask 应用程序时遇到了问题。我正在尝试使用 Visual Studio 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()

这是调试的配置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
    }

问题是,每次我在调试模式下运行它时,程序都不会执行我的代码,而是显示这个错误: Error: module 'application_hook' has no attribute 'FlaskApp' 为什么会发生?提前致谢

多一点追溯:

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

2 回答 2

1

要修复“错误:模块 'application_hook' has no attribute 'FlaskApp'”,请通过设置FLASK_APP启动应用程序的主文件的名称来更新 launch.json(例如,app.py 或 main.py)。

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

  • 模块应从更改flaskconnexion
  • args
    • 删除--no-debugger
    • 将路径添加到您的规范文件
    • 添加--port和端口号

启动.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
        }
    ]
}

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

主模块(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)
于 2020-10-07T12:46:45.940 回答
0

不知道为什么!!!但是,我正在做一个 Flask/Connexion 项目。两个月前,我一直在努力尝试在 VSCode 上进行调试。当它在 PyCharm 上按预期工作并且零痛苦时,我放弃了。现在,也许是由于一些 Linux/VSCode?PyCharm 更新,相同的源代码不再可以使用 PyCharm 进行“调试”,并且正在使用最简单的启动器在 VSCode 上工作:

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

不要问我为什么!这是相同的源代码!是的,我正在使用 venv。

于 2021-07-29T02:52:27.000 回答