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)
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:
raises Error because the file is not named main.py! If you have it as
my_fastapi_server.py, you run it as follows:Adding another snippet as the ones posted here were pretty cryptic for me.
With the following files:
I needed my
debug_server.pyto be:That way the debug auto-reload server did work properly and there’s nothing related to it in the
/appfolder.Try this.
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
uvicornif you want to run with multiple workers. Please read uvicorn documentation.You can use pathlib to avoid this problem:
This depends on your file being named
api.py. Using__main__.appmeans you can rename yourapp.pyfile to anything and it will still work.Further reading https://docs.python.org/3/library/__main__.html