安装VSCode的FastAPI Extension插件

断点调试
- 使用
debugpy启动main.py
# 安装 debugpy
uv add --dev debugpy
# Makefile
.PHONY: dev debug
dev:
export PYTHONDONTWRITEBYTECODE=1; \
uv run --package web-service fastapi dev apps/web-service/app/main.py --port 8080
debug:
export PYTHONDONTWRITEBYTECODE=1; \
uv run --package web-service python -m debugpy --listen 0.0.0.0:5678 --wait-for-client -m fastapi dev apps/web-service/app/main.py --port 8000
windows
# Makefile
.PHONY: dev debug
dev:
set PYTHONDONTWRITEBYTECODE=1 && uv run --package web-service fastapi dev apps/web-service/app/main.py --port 8080
debug:
set PYTHONDONTWRITEBYTECODE=1 && uv run --package web-service python -m debugpy --listen 0.0.0.0:5678 --wait-for-client -m fastapi dev apps/web-service/app/main.py --port 8000
# 启动调试服务器
make debug
- 启动调试客户端
{
// 使用 IntelliSense 了解相关属性。
// 悬停以查看现有属性的描述。
// 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Attach to make run",
"type": "debugpy",
"request": "attach", // 使用附加模式
"connect": {
"host": "localhost",
"port": 5678
}
}
]
}

常见错误
File "d:\phpstudy_pro\WWW\my\python\my-fastapp\.venv\Lib\site-packages\debugpy\server\api.py", line 131, in debug
return func(address, settrace_kwargs, **kwargs)
File "d:\phpstudy_pro\WWW\my\python\my-fastapp\.venv\Lib\site-packages\debugpy\server\api.py", line 260, in listen
raise RuntimeError(str(endpoints["error"]))
RuntimeError: Can't listen for client connections: [WinError 10048] 通常每个套接字地址(协议/网络地址/端口)只允许使用一次。
make: *** [Makefile:9: debug] Error 1
# 1. 查找占用 5678 端口的进程 PID
netstat -ano | findstr :5678
# 2. 假设上面查出来的 PID 是 12345,强制结束它(请替换为实际 PID)
taskkill /PID 12345 /F




