【问题标题】:Debug FastAPI application in VSCode在 VSCode 中调试 FastAPI 应用程序
【发布时间】:2020-05-28 23:56:33
【问题描述】:

我正在尝试调试使用 FastAPI (uvicorn) 的应用程序(Web api) 我也在用诗歌,在vscode中设置projev虚拟环境。

我阅读了this 设置uvicorn 和this one 设置vscode 的教程,但我认为我在设置时做错了。

我尝试将 launch.json 设置为 python: modulepython: current file

问题似乎是当我运行调试时它无法识别项目结构导致它在导入语句中停止并出现此错误:

Exception has occurred: ImportError
attempted relative import with no known parent package

这是我当前的 launch.json 配置:

"configurations": [
    {
        "name": "Python: local debug",
        "type": "python",
        "request": "launch",
        "program": "${workspaceFolder}/src/topic_service/service/__init__.py",
        "args" : ["--port", "8000"]
    },
]

我还尝试添加一个 .env 文件设置 PYTHONPATH:

PYTHONPATH=.:${PYTHONPATH}

我在本地运行应用程序如下:

poetry run uvicorn src.main:app --port 8080 --reload

有谁知道如何正确设置 vscode 以调试 uvicorn 应用程序

谢谢

更新: 我也试过this article 说的。调试器似乎启动了,但什么也没发生(没有触发断点)

【问题讨论】:

    标签: visual-studio-code fastapi uvicorn


    【解决方案1】:

    对我来说,使用此配置:

    在 VSCode 的 Debug 部分,选择 create launch.json 选项。可能它会在您的根文件夹资源管理器中的 .vscode 文件夹上打开 launch.json like this,在 launch.json 里面放这个:

    "version": "0.2.0",
      "configurations": [
        {
          "name": "Python: FastAPI",
          "type": "python",
          "request": "launch",
          "module": "uvicorn",
          "cwd": "${workspaceFolder}/<folder to your main.py>",
          "args": [
            "main:app",
            "--reload",
            "--port", //these arg are optional
            "3003"
          ]
        }
      ]
    }
    

    现在,只需运行您的调试器,祝您有美好的一天!

    编辑: cwd 确保您的调试器将找到您的 main.py 文件的正确路径。因此,对于使用多个调试器或在带有 launch.json 的 vsCode 之外使用的人来说,使用它是一个不错的选择。

    【讨论】:

    • 我个人在 .vscode 目录中的 VSCODE 上使用带有 launch.json 的调试器。但是有一种方法可以在 vscode 中使用它,也许你的调试器会遇到问题,找到你的 main.py 所在的位置。因此,如果您只需要运行您的 fastAPI 应用程序,最简单的方法就是在调试器中使用 cwd。
    • 添加cwd 的解释应该进入答案。您可以使用答案底部的Edit 按钮进行更新。
    【解决方案2】:

    快速简便的方法:启动调试器F5,然后选择FastAPI Debug Configuration: (p.s. 这适用于 VSCode Insiders;还没有在普通版本上尝试过)

    【讨论】:

      【解决方案3】:

      同样,您在开发 FastAPI 应用程序期间为uvicorn module 提供必要的args,您需要使用相应的值配置位于.vscode 目录中的launch.json .

      我为debug FastAPI in VS Code here写了一篇关于自定义项目配置的文章

      假设您发出以下命令以在 uvicorn 服务器上运行 FastAPI,参数如下所述

      uvicorn main:app --reload --port 8000
      

      那么你的 launch.json 应该有 module ,其值为 uvicorn 并且每个 args 以空格分隔作为 args 数组的项目。

      "module": "uvicorn",
      "type": "python",
      "request": "launch",
      "args": [
          "main:app",
          "--reload",
          "--port",
          "8000"
      ],
      "env": {
          "usersecret": "some$Ecret",
      }
      

      您可以在.vscode 中拥有这个launch.json 文件,然后根据您的项目结构修改JSON 配置中的args 数组。

      {
          // 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: FastAPI",
                  "type": "python",
                  "request": "launch",
                  "module": "uvicorn",
                  "env": {
                      "db_username": "postgres",
                      "db_password": "secret",
                      "host_server": "localhost",
                      "database_name": "fastapi",
                      "ssl_mode": "prefer",
                      "db_server_port": "5432"
                  },
                  "args": [
                      "main:app",
                      "--reload",
                      "--port",
                      "8000"
                  ]
              }
          ]
      }
      

      【讨论】:

      • 另外,您还可以指定从哪里获取.env文件,而不是在“env”属性中声明每个环境变量,只需添加属性-> "envFile": "./some_folder/foo/.env"
      【解决方案4】:

      我的调试方式很基础,希望对你有帮助 我有这个配置的 .py 文件:

      import uvicorn
      from app.main import api
      
      if __name__ == "__main__":
          dev = 1
          if dev==0:
              #use this one
              uvicorn.run(api, host="127.0.0.1", port=5000, log_level="info")
          if dev == 1:
              #or this one
              uvicorn.run('app.main:api', host="127.0.0.1", port=5000, log_level="info", reload=True, debug=True)
          if dev == 2:
              uvicorn.run('app.main:api', host="127.0.0.1", port=5000, log_level="info", workers=2)
      

      并使用 vscode 调试器运行文件,重要的是使用 debug 标志运行应用程序,否则调试器会跳过断点(至少在我的情况下)

      【讨论】:

        【解决方案5】:

        试试这个配置。

        {
            "name": "Python: Module",
            "type": "python",
            "request": "launch",
            "module": "uvicorn",
            "args": ["src.main:app","--reload"]
        }
        

        【讨论】:

          猜你喜欢
          • 2016-07-06
          • 1970-01-01
          • 1970-01-01
          • 2019-01-12
          • 2016-08-28
          • 2023-03-20
          • 2018-09-05
          • 2020-10-23
          • 2017-07-27
          相关资源
          最近更新 更多