Python uvicorn:术语“uvicorn”不被识别为 cmdlet、函数、脚本文件的名称

Eri*_*ven 25 python fastapi uvicorn

晚上好,

\n

我正在使用 python 3.9 并尝试根据互联网上的文档在 Windows 10 Pro 上运行新的 FastAPI 服务https://www.uvicorn.org/我执行了以下语句

\n
pip install uvicorn pip install uvicorn[standard]\n
Run Code Online (Sandbox Code Playgroud)\n

创建示例文件 app.py

\n
from fastapi import FastAPI\n\napp = FastAPI()\n\n\n@app.get("/")\nasync def root():\n    return {"message": "Hello World"}\n
Run Code Online (Sandbox Code Playgroud)\n

但是当我运行下面的代码时:

\n
uvicorn main:app --reload\n\n\nuvicorn : The term \'uvicorn\' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify t\nhat the path is correct and try again.\nAt line:1 char:1\n+ uvicorn\n+ ~~~~~~~\n    + CategoryInfo          : ObjectNotFound: (uvicorn:String) [], CommandNotFoundException\n    + FullyQualifiedErrorId : CommandNotFoundException\n
Run Code Online (Sandbox Code Playgroud)\n

我还在环境设置中添加了Python的路径

\n

我还重新安装了Python 3.9,并将默认安装路径设置为c:\\ProgramFiles\\Python39,该路径现在也包含在系统环境和用户环境设置中。

\n

在此输入图像描述

\n

如果我再次运行 pip install uvicorn ,它会显示以下语句:

\n
\xce\xbb pip install uvicorn\nDefaulting to user installation because normal site-packages is not writeable\nRequirement already satisfied: uvicorn in c:\\users\\username\\appdata\\roaming\\python\\python39\\site-packages (0.12.2)\nRequirement already satisfied: h11>=0.8 in c:\\users\\username\\appdata\\roaming\\python\\python39\\site-packages (from uvicorn) (0.11.0)\nRequirement already satisfied: click==7.* in c:\\users\\username\\appdata\\roaming\\python\\python39\\site-packages (from uvicorn) (7.1.2)\nWARNING: You are using pip version 20.2.3; however, version 20.2.4 is available.\nYou should consider upgrading via the \'c:\\program files\\python39\\python.exe -m pip install --upgrade pip\' command.\n
Run Code Online (Sandbox Code Playgroud)\n

非常感谢

\n

埃里克

\n

lux*_*ux7 65

您还可以uvicorn使用以下命令运行:

python -m uvicorn main:app --reload
Run Code Online (Sandbox Code Playgroud)


Eri*_*ven 9

Python 将其脚本安装在以下路径的脚本文件夹中: c:\users\username\appdata\roaming\python\python39\scripts
将该路径放入系统和用户环境变量中。这将解决问题。


小智 5

首先,你必须跑

虚拟环境

然后安装代码

pip 安装 fastapi uvicorn