TypeError语言 - Fastapi样例在加载时失败



我是第一次尝试fastapi,并使用文档中的示例代码。

from fastapi import FastAPI
app = FastAPI()

@app.get("/")
async def root():
return {"message": "Hello World"}

我还在执行

之前安装了所需的软件包
pip install "fastapi[all]" 
pip install uvicorn

And execute failed with TypeError.

uvicorn model_sm:app --reload
INFO:     Will watch for changes in these directories: ['/Users/name/workplace/flask']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [24303] using StatReload
Process SpawnProcess-1:
Traceback (most recent call last):
File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 315, in _bootstrap
self.run()
File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/multiprocessing/process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "/usr/local/lib/python3.9/site-packages/uvicorn/_subprocess.py", line 76, in subprocess_started
target(sockets=sockets)
File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 59, in run
return asyncio.run(self.serve(sockets=sockets))
File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/runners.py", line 44, in run
return loop.run_until_complete(main)
File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/asyncio/base_events.py", line 647, in run_until_complete
return future.result()
File "/usr/local/lib/python3.9/site-packages/uvicorn/server.py", line 66, in serve
config.load()
File "/usr/local/lib/python3.9/site-packages/uvicorn/config.py", line 471, in load
self.loaded_app = import_from_string(self.app)
File "/usr/local/lib/python3.9/site-packages/uvicorn/importer.py", line 21, in import_from_string
module = importlib.import_module(module_str)
File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/Users/name/workplace/flask/model_sm.py", line 2, in <module>
from fastapi import FastAPI
File "/usr/local/lib/python3.9/site-packages/fastapi/__init__.py", line 7, in <module>
from .applications import FastAPI as FastAPI
File "/usr/local/lib/python3.9/site-packages/fastapi/applications.py", line 16, in <module>
from fastapi import routing
File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 478, in <module>
class APIRouter(routing.Router):
File "/usr/local/lib/python3.9/site-packages/fastapi/routing.py", line 497, in APIRouter
lifespan: Optional[Lifespan[Any]] = None,
File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 277, in inner
return func(*args, **kwds)
File "/usr/local/Cellar/python@3.9/3.9.15/Frameworks/Python.framework/Versions/3.9/lib/python3.9/typing.py", line 768, in __getitem__
_check_generic(self, params, len(self.__parameters__))
File "/usr/local/lib/python3.9/site-packages/typing_extensions.py", line 97, in _check_generic
raise TypeError(f"{cls} is not a generic class")
TypeError: typing.Union[typing.Callable[[ForwardRef('Starlette')], typing.AsyncContextManager[NoneType]], typing.Callable[[ForwardRef('Starlette')], typing.AsyncContextManager[typing.Mapping[str, typing.Any]]]] is not a generic class
我在搜索中找不到任何有用的东西。有什么好办法吗?
pip install fastapi
  • 尝试使用上面的命令安装fastapi,它为我工作。

  • 如果你使用的是macOS或linux,你应该使用以下命令。

    pip3 install fastapi

  • 在运行此脚本之前,打开终端并导入FastAPI以确保其正常工作。然后运行这个脚本。

发现uvicorn命令依赖于Python3.9来执行。所以我使用pip3.9重新安装了下面所有的包,它修复了这个问题。

pip3.9 install typing-extensions
pip3.9 install uvicorn
pip3.9 install fastapi

相关内容

  • 没有找到相关文章

最新更新