Uvicorn: Deployment documentation about Running programmatically not correct for reload

Created on 7 May 2019  路  4Comments  路  Source: encode/uvicorn

The deployment page gives the following example

if __name__ == "__main__":
    uvicorn.run(app, host="127.0.0.1", port=5000, log_level="info", reload=True)

This does not work for me, it gives the warning

WARNING:uvicorn:auto-reload only works when app is passed as an import string.

What works for me is something like

if __name__ == "__main__":
    uvicorn.run("module:app", host="127.0.0.1", port=5000, log_level="info", reload=True)

Most helpful comment

any .py file is a module. so if your entry file is main.py and "app" is the ASGI app then use "main" as the module name. ie. "main:app"

All 4 comments

the latter breaks with

ModuleNotFoundError: No module named 'aiofiles'

so had to

pip install aiofiles

Thanks, Patrick!

What should module be set to? When using your example, I get:

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

My layout is simply app/main.py, and I am running it as python3 app/main.py.

any .py file is a module. so if your entry file is main.py and "app" is the ASGI app then use "main" as the module name. ie. "main:app"

That works, thanks!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tlc picture tlc  路  3Comments

gnat picture gnat  路  6Comments

ipmb picture ipmb  路  7Comments

rspadim picture rspadim  路  5Comments

tomchristie picture tomchristie  路  4Comments