未找到 'uvloop>=0.14.0' 发行版,这是 uvicorn 所需要的

Xue*_*era 6 python uvloop fastapi uvicorn

我是学习 FastAPI 的新手,一开始就陷入困境。我不断收到以下错误:

(venv) root@Xue:/home/proyectos/FastAPI# uvicorn main.py:app --reload
Traceback (most recent call last):
  File "/usr/bin/uvicorn", line 6, in <module>
    from pkg_resources import load_entry_point
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3254, in <module>
    def _initialize_master_working_set():
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3237, in _call_aside
    f(*args, **kwargs)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 3266, in _initialize_master_working_set
    working_set = WorkingSet._build_master()
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 584, in _build_master
    ws.require(__requires__)
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 901, in require
    needed = self.resolve(parse_requirements(requirements))
  File "/usr/lib/python3/dist-packages/pkg_resources/__init__.py", line 787, in resolve
    raise DistributionNotFound(req, requirers)
pkg_resources.DistributionNotFound: The 'uvloop>=0.14.0' distribution was not found and is required by uvicorn
Run Code Online (Sandbox Code Playgroud)

这是我的Python代码:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def home():
    return {'Hello': 'World'}
Run Code Online (Sandbox Code Playgroud)

我已经尝试过pip install -U uvloop pip, pip install uvloop==0.14.0但没有修复。

小智 6

我尝试使用以下命令运行,这对我有用:

python -m uvicorn main:app --reload

这里的 main 是你的文件名


小智 0

我有这样的问题。首先,激活你的 venv:

source venv/bin/activate
Run Code Online (Sandbox Code Playgroud)

要求中的冻结和安全点列表:

pip freeze > requirements.txt
Run Code Online (Sandbox Code Playgroud)

卸载:

pip uninstall -r requirements.txt -y
Run Code Online (Sandbox Code Playgroud)

停用:

rm -r venv/
Run Code Online (Sandbox Code Playgroud)

之后你必须重新制作 venv:

python3 -m venv venv
Run Code Online (Sandbox Code Playgroud)

并安装所有必要的文件。