How can I run the fast-api server using Pycharm?

Solution:

You can call uvicorn from your main.py as,

import uvicorn
from fastapi import FastAPI

app = FastAPI()


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


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

Now, you can start the server by python command,

python main.py

and now, you can set the script path in Pycharm's config enter image description here

Note: This can be also useful in setting breakpoints(for debugging) using Pycharm.


Try to call uvicorn inside your code. e.g:

from fastapi import FastAPI
import uvicorn

app = FastAPI()


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

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

Reference