【问题标题】:VSCode and FastAPI debugging - doesn't break within routesVSCode 和 FastAPI 调试 - 不会在路由中中断
【发布时间】: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


    【解决方案1】:

    原因是虽然代码中定义了“root()”方法,但我们并没有在代码中调用这个方法。调试时,调试器没有执行方法中的代码。

    请尝试在代码中添加调用方法“root()”的语句:

    【讨论】:

    • 感谢您抽出宝贵时间,但这不是我要找的。我正在寻找的是当用户请求 URL 时,断点会触发。如果我在浏览器中转到localhost:5002,它会执行该方法,因为我得到了我期望的响应({“hello world”:'ba'}),但它不会在断点处暂停。当我使用 Flask 时,它会在触发请求时按预期执行行为。如果我想调试特定参数,我想我可以创建一个文件来调用该函数,但这似乎是一种迂回的方式。
    • @SSShah 你明白了吗?
    • @Curtwagner1984 在帖子底部查看我的编辑。从来没有花更多时间研究它(并且不再使用 FastAPI)
    猜你喜欢
    • 2020-05-28
    • 2023-03-20
    • 2022-09-23
    • 2021-04-16
    • 2019-03-17
    • 2020-10-04
    • 2021-12-24
    • 2021-07-22
    • 1970-01-01
    相关资源
    最近更新 更多