我在启动Flask应用程序时遇到了一个问题。我正试图用Visual Studio代码在调试模式下运行该应用程序,但它无法正常运行。
这是主要模块代码:
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:UsersfeleDocumentsGit> cd 'c:UsersfeleDocumentsGit'; & 'c:UsersfeleDocumentsGit.venvScriptspython.exe' 'c:Usersfele.vscodeextensionsms-python.python-2020.9.112786pythonFileslibpythondebugpylauncher' '51724' '--' '-m' 'flask' 'run' '--no-debugger'
* Serving Flask app "application_hook:FlaskApp('dev')"
* Environment: development
* Debug mode: off
C:UsersfeleDocumentsGit/connector/config/Git.ini
C:UsersfeleDocumentsGit.venvlibsite-packagesconnexionappsflask_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:UsersfeleDocumentsGit>
修复"错误:模块"application_hook"没有属性"FlaskApp">,通过将FLASK_APP
设置为启动应用程序的主文件的名称(例如app.py或main.py(来更新launch.json。
此外,由于您使用connexion,launch.json需要更新如下:
- 模块应从
flask
更改为connexion
args
:- 删除
--no-debugger
- 将路径添加到规范文件
- 添加
--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
}
]
}
在主模块中,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)
我不知道为什么!!!但是,我正在做一个Flask/Conecexion项目。两个月前,我一直在努力在VSCode上调试它。当它在PyCharm上毫无痛苦地发挥作用时,我放弃了。现在,也许是因为一些Linux/VCode?PyCharm更新,相同的源代码不再可以用PyCharm"调试",而是用最简单的启动器:处理VSCode
"configurations": [
{
"name": "Python: Debug",
"type": "python",
"request": "launch",
"program": "app.py",
"console": "integratedTerminal"
}
]
别问我为什么!这是相同的源代码!是的,我用的是venv。