创建了以下fastapi的hello world程序,项目启动时,提示错误:

- ERROR:    Error loading ASGI app. Could not import module "main".

Helloworld.py:

from fastapi import FastAPI

import uvicorn

app = FastAPI()

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

if __name__ == '__main__':
    uvicorn.run(app='main:app', host="127.0.0.1", port=8080, reload=True)

调查发现,在uvicorn.run函数声明app的文件路径应该是 Helloworld:app,而不是main:app

将'main:app' 修改为‘Helloworld:app’即可正常运行:

- python Helloworld.py --reload

07-01 13:20