FastAPI 不断抛出加载错误(ASGI 应用程序)

FastAPI keeps throwing an error loading (ASGI app)

文件名是 main.py 我随时 运行 uvicorn main:app --reload 我收到代码下的错误。

from fastapi import FastAPI

app = FastAPI()


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

错误

INFO:     Will watch for changes in these directories: ['/Users/Documents/main.py']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [58745] using statreload
ERROR:    Error loading ASGI app. Could not import module "main".

你 运行 命令 uvicorn main:app --reload 在与文件 main.py 相同的目录中吗?可能那是你的问题。 我测试了你的代码,它对我有用。

检查你的文件名是否应该是 main 或者如果它的名字是 'example' 你应该写:uvicorn example:app --reload