You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MSYS2环境中FastAPI命令无法识别的问题及正确安装咨询

在MSYS2 MinGW x64中FastAPI命令无法识别的问题解决

问题背景

操作环境为MSYS2 MinGW x64 shell,已执行以下命令安装组件:

pacman -S mingw-w64-x86_64-python
pacman -S mingw-w64-x86_64-python-pip
pacman -S mingw-w64-x86_64-python-fastapi

同时执行了pip install fastapi,返回显示依赖已存在。编写test.py内容如下:

from fastapi import FastAPI

meine_coole_rest_api = FastAPI()

@meine_coole_rest_api.get("/")
async def wurzel_pfad():
   return {"coole_nachricht" : "Fast API works"}

执行官方教程命令fastapi dev test.py时,收到错误:

bash: fastapi: command not found

原因分析

  1. pacman安装的mingw-w64-x86_64-python-fastapi仅包含FastAPI核心库,不附带fastapi命令行工具(即fastapi-cli)
  2. 直接执行pip install fastapi时,可能将包安装到了Windows系统的Python环境,而非MSYS2 MinGW的Python路径,导致shell无法定位到命令

解决方法

方法一:通过pacman安装完整工具链

执行以下命令,安装FastAPI命令行工具和运行依赖:

pacman -S mingw-w64-x86_64-python-fastapi-cli mingw-w64-x86_64-python-uvicorn

安装完成后,直接运行:

fastapi dev test.py

方法二:使用MinGW的pip安装完整包

先确认当前pip属于MSYS2 MinGW环境:

which pip

输出应为/mingw64/bin/pip,确认后执行:

pip install fastapi[all]

该命令会安装FastAPI及其全套依赖(包括fastapi-cli和uvicorn),之后即可正常运行fastapi dev test.py

替代方案:直接用uvicorn启动

如果不想安装fastapi-cli,可直接用uvicorn命令启动服务:

uvicorn test.py:meine_coole_rest_api --reload

内容的提问来源于stack exchange,提问作者flori10

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 03:58:29