記載内容
FastAPIについて、VSCodeとdebugpyを使ったリモートデバッグ方法を記載します。
FastAPIは下記のように起動します。これでどうやって、main.pyにリモートデバッガーを接続できるのか迷ったので、記録を残します。
uvicorn main:app --reload
FastAPIの起動方法
単純にuvicornをモジュールとして実行する形で、main.pyをリモートデバッグできました。
python -m debugpy --listen 0.0.0.0:5678 --wait-for-client -m uvicorn main:app --reload
VSCodeのlaunch.jsonの設定
通常通り設定すればOKです。
{
"version": "0.1.0",
"configurations": [
{
"name": "FastAPI Remote Debug",
"type": "python",
"request": "attach",
"port": 5678,
"host": "localhost",
"pathMappings": [
{
"localRoot": "${workspaceFolder}",
"remoteRoot": "/hogehoge"
}
]
}
]
}