{"id":768,"date":"2026-04-01T09:58:00","date_gmt":"2026-04-01T01:58:00","guid":{"rendered":"https:\/\/mitongxue.cn\/?p=768"},"modified":"2026-07-24T13:27:30","modified_gmt":"2026-07-24T05:27:30","slug":"fastapi%e8%b0%83%e8%af%95","status":"publish","type":"post","link":"https:\/\/mitongxue.cn\/index.php\/2026\/04\/01\/fastapi%e8%b0%83%e8%af%95\/","title":{"rendered":"FastAPI\u8c03\u8bd5"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5b89\u88c5<code>VSCode<\/code>\u7684<code>FastAPI Extension<\/code>\u63d2\u4ef6<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"286\" src=\"https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-11-1024x286.png\" alt=\"\" class=\"wp-image-770\" srcset=\"https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-11-1024x286.png 1024w, https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-11-300x84.png 300w, https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-11-768x215.png 768w, https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-11.png 1256w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">\u65ad\u70b9\u8c03\u8bd5<\/h2>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u4f7f\u7528<code>debugpy<\/code>\u542f\u52a8<code>main.py<\/code><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># \u5b89\u88c5 debugpy<br>uv add --dev debugpy<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># Makefile<br>\u200b<br>.PHONY: dev debug<br>\u200b<br>dev:<br>    export PYTHONDONTWRITEBYTECODE=1; \\<br>    uv run --package web-service fastapi dev apps\/web-service\/app\/main.py --port 8080<br>\u200b<br>debug:<br>    export PYTHONDONTWRITEBYTECODE=1; \\<br>    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<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">windows<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Makefile<br>\u200b<br>.PHONY: dev debug<br>\u200b<br>dev:<br>    set PYTHONDONTWRITEBYTECODE=1 &amp;&amp; uv run --package web-service fastapi dev apps\/web-service\/app\/main.py --port 8080<br>\u200b<br>debug:<br>    set PYTHONDONTWRITEBYTECODE=1 &amp;&amp; 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<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u200b<br># \u542f\u52a8\u8c03\u8bd5\u670d\u52a1\u5668<br>make debug<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li>\u542f\u52a8\u8c03\u8bd5\u5ba2\u6237\u7aef<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>{<br> &nbsp;\/\/ \u4f7f\u7528 IntelliSense \u4e86\u89e3\u76f8\u5173\u5c5e\u6027\u3002<br> &nbsp;\/\/ \u60ac\u505c\u4ee5\u67e5\u770b\u73b0\u6709\u5c5e\u6027\u7684\u63cf\u8ff0\u3002<br> &nbsp;\/\/ \u6b32\u4e86\u89e3\u66f4\u591a\u4fe1\u606f\uff0c\u8bf7\u8bbf\u95ee: https:\/\/go.microsoft.com\/fwlink\/?linkid=830387<br> &nbsp;\"version\": \"0.2.0\",<br> &nbsp;\"configurations\": &#91;<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp;\"name\": \"Attach to make run\",<br> &nbsp; &nbsp; &nbsp;\"type\": \"debugpy\",<br> &nbsp; &nbsp; &nbsp;\"request\": \"attach\", \/\/ \u4f7f\u7528\u9644\u52a0\u6a21\u5f0f<br> &nbsp; &nbsp; &nbsp;\"connect\": {<br> &nbsp; &nbsp; &nbsp; &nbsp;\"host\": \"localhost\",<br> &nbsp; &nbsp; &nbsp; &nbsp;\"port\": 5678<br> &nbsp; &nbsp;  }<br> &nbsp;  }<br>  ]<br>}<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"776\" src=\"https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-12-1024x776.png\" alt=\"\" class=\"wp-image-771\" srcset=\"https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-12-1024x776.png 1024w, https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-12-300x227.png 300w, https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-12-768x582.png 768w, https:\/\/mitongxue.cn\/wp-content\/uploads\/2026\/07\/image-12.png 1152w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5e38\u89c1\u9519\u8bef<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n  File \"d:\\phpstudy_pro\\WWW\\my\\python\\my-fastapp\\.venv\\Lib\\site-packages\\debugpy\\server\\api.py\", line 131, in debug\n    return func(address, settrace_kwargs, **kwargs)\n  File \"d:\\phpstudy_pro\\WWW\\my\\python\\my-fastapp\\.venv\\Lib\\site-packages\\debugpy\\server\\api.py\", line 260, in listen\n    raise RuntimeError(str(endpoints&#91;\"error\"]))\nRuntimeError: Can't listen for client connections: &#91;WinError 10048] \u901a\u5e38\u6bcf\u4e2a\u5957\u63a5\u5b57\u5730\u5740(\u534f\u8bae\/\u7f51\u7edc\u5730\u5740\/\u7aef\u53e3)\u53ea\u5141\u8bb8\u4f7f\u7528\u4e00\u6b21\u3002\nmake: *** &#91;Makefile:9: debug] Error 1<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code># 1. \u67e5\u627e\u5360\u7528 5678 \u7aef\u53e3\u7684\u8fdb\u7a0b PID\nnetstat -ano | findstr :5678\n\n# 2. \u5047\u8bbe\u4e0a\u9762\u67e5\u51fa\u6765\u7684 PID \u662f 12345\uff0c\u5f3a\u5236\u7ed3\u675f\u5b83\uff08\u8bf7\u66ff\u6362\u4e3a\u5b9e\u9645 PID\uff09\ntaskkill \/PID 12345 \/F<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b89\u88c5VSCode\u7684FastAPI Extensio..<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[52,14],"tags":[],"class_list":["post-768","post","type-post","status-publish","format-standard","hentry","category-python","category-back"],"featured_image_urls":{"full":"","thumbnail":"","medium":"","medium_large":"","large":"","1536x1536":"","2048x2048":""},"author_info":{"info":["\u9648 \u67d0\u4eba"]},"category_info":"<a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/python\/\" rel=\"category tag\">Python<\/a> <a href=\"https:\/\/mitongxue.cn\/index.php\/category\/back\/\" rel=\"category tag\">\u540e\u7aef<\/a>","tag_info":"\u540e\u7aef","comment_count":"0","_links":{"self":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/comments?post=768"}],"version-history":[{"count":1,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/768\/revisions"}],"predecessor-version":[{"id":772,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/posts\/768\/revisions\/772"}],"wp:attachment":[{"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/media?parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/categories?post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mitongxue.cn\/index.php\/wp-json\/wp\/v2\/tags?post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}