创建了以下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
文章描述了一个使用FastAPI构建的Helloworld程序在启动时遇到的错误,错误信息为无法加载ASGI应用,模块导入失败。问题出在uvicorn.run函数中,app的文件路径参数应指定为Helloworld:app而非main:app。修正这一错误后,程序可以正常运行。
1126

被折叠的 条评论
为什么被折叠?



