fastapi: uvicorn can not start FastAPI with example settings

import uvicorn
from fastapi import FastAPI
app = FastAPI(title='MADS API')
uvicorn.run(app, host='0.0.0.0', port=8127, workers=2)
WARNING:  You must pass the application as an import string to enable 'reload' or 'workers'.

than it quits with SystemError 1

Corresponding ticket in uvicorn

Versions:

$ pip3 list |grep "uvicorn\|fastapi"
fastapi                          0.55.1       
uvicorn                          0.11.5 

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 3
  • Comments: 15 (2 by maintainers)

Most upvoted comments

Why do you say it is a bug? It is your first try at FastAPI?

The answer I was searching for is here: You can actually start it the way I wanted it, but make sure to name it correctly:

uvicorn.run("main:app", host='0.0.0.0', port=8127, workers=2)
ERROR:    Error loading ASGI app. Could not import module "main".

raises Error because the file is not named main.py! If you have it as my_fastapi_server.py, you run it as follows:

uvicorn.run("my_fastapi_server:app", host='0.0.0.0', port=8127, workers=2)

Adding another snippet as the ones posted here were pretty cryptic for me.

With the following files:

app\main.py    # Containing an app object
debug_server.py

I needed my debug_server.py to be:

import uvicorn

# Importing app here makes the syntax cleaner as it will be picked up by refactors
from app.main import app

if __name__ == "__main__":
    uvicorn.run("debug_server:app", host="0.0.0.0", port=80, reload=True)

That way the debug auto-reload server did work properly and there’s nothing related to it in the /app folder.

Try this.

import uvicorn
from fastapi import FastAPI

app = FastAPI()


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


if __name__ == "__main__":
    uvicorn.run("__main__:app", host="0.0.0.0", port=8000, reload=True, workers=2)

This is intended and not related to fastapi:

https://github.com/encode/uvicorn/blob/9b92925a352b9743c5cfcef4a65e74a81a1bad4f/uvicorn/main.py#L343

You need to run it with command line uvicorn if you want to run with multiple workers. Please read uvicorn documentation.

The answer I was searching for is here: You can actually start it the way I wanted it, but make sure to name it correctly:

uvicorn.run("main:app", host='0.0.0.0', port=8127, workers=2)
ERROR:    Error loading ASGI app. Could not import module "main".

raises Error because the file is not named main.py! If you have it as my_fastapi_server.py, you run it as follows:

uvicorn.run("my_fastapi_server:app", host='0.0.0.0', port=8127, workers=2)

You can use pathlib to avoid this problem:

from pathlib imprt Path

uvicorn.run(f"{Path(__file__).stem}:app", host='0.0.0.0', port=8127, workers=2)

What also worked for me was this:

inside the api.py file

if __name__ == "__main__":
    run(app="api:app", reload=True, port=8080, host="0.0.0.0")

This depends on your file being named api.py. Using __main__.app means you can rename your app.py file to anything and it will still work.

Further reading https://docs.python.org/3/library/__main__.html