【发布时间】:2021-04-24 05:24:38
【问题描述】:
刚开始使用 FastAPI,但在尝试让它识别 VSCode 调试器中的断点时遇到了问题。奇怪的是,它确实成功地打破了路线中不包含的线路
直接从教程中拉取:https://fastapi.tiangolo.com/tutorial/debugging/
import uvicorn
from fastapi import FastAPI
app = FastAPI() # breakpoint here works on launching file
print('here') # breakpoint here works on launching file
@app.get("/")
def root():
a = "a" # breakpoint here does NOT work
b = "b" + a
return {"hello world": b} # returns data successfully
if __name__ == "__main__":
uvicorn.run(app, host="0.0.0.0", port=5002)
如上所述,路由外的行工作正常,当我转到地址时,我成功获取数据,但路由内的断点没有触发。不知道我在这里缺少什么。尝试了Debug FastAPI application in VSCode的各种解决方案
我不确定这是否会有所不同,但这也是通过 VSCode 上的 Remote-SSH 扩展运行的(我的代码在 gcloud VM 上)。也许这是有贡献的,但同样,其他断点触发良好。
我的launch.json:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
]
}
编辑:如果我将“def”替换为“async def”,看起来它确实有效,尽管我试图让它与 SQLite DB 一起使用,并且根据这个页面:https://fastapi.tiangolo.com/tutorial/sql-databases/ 它建议使用“def”默认情况下(带有配置为使用 async def 的选项)。我需要进一步研究区别。
【问题讨论】:
标签: python visual-studio-code vscode-debugger fastapi