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

PythonPycharmFastapi

Python Problem Overview


I have a simple API function as below,

from fastapi import FastAPI

app = FastAPI()


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

I am starting the server using uvicorn command as,

uvicorn main:app

Since we are not calling any python file directly, it is not possible to call uvicorn command from Pycharm.

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

Python Solutions


Solution 1 - Python

Method-1: Run FastAPI by calling uvicorn.run(...)

In this case, your minimal code will be as follows,

# main.py




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)

if name == "main": uvicorn.run(app, host="0.0.0.0", port=8000)

Normally, you'll start the server by running the following command,

python main.py
Pycharm Setup

For this setup, and now, you can set the script path in Pycharm's config

Pycharm-uvicorn.run

Notes
  • Script Path: path to the FastAPI script
  • Python Interpreter: Choose your interpreter/virtual environment
  • Working Directory: Your FastAPI project root

Method-2: Run FastAPI by calling uvicorn command

In this case, your minimal code will be as follows,

# main.py




from fastapi import FastAPI




app = FastAPI()




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

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

Normally, you'll start the server by running the following command,

uvicorn main:app --reload
Pycharm Setup

For this setup, and now, you can set the script path in Pycharm's config

Pycharm-uvicorn.command

Notes
  • Module name: set to uvicorn

  • [Optional] Script: Path to uvicorn binary. You will get the path by executing the command, which uvicorn , inside your environment. (See this image)

  • Parameters: The actual parameters of uvicorn command

  • Python Interpreter: Choose your interpreter/virtual environment

  • Working Directory: Your FastAPI project root

Solution 2 - Python

You can do it without adding code to main.py

  1. In target to run instead of Script path choose Module name
  2. In Module name type uvicorn
  3. In parameters app.main:app --reload --port 5000

enter image description here

Solution 3 - Python

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

Solution 4 - Python

Another example, this might be helpful to someone.

# fastapi_demo.py

import uvicorn
from fastapi import FastAPI, Response

app = FastAPI()

@app.route('/', methods=['POST'])
def demo(request):
    try:
        print(request)
    except Exception as e:
        print(e)
    return Response(content='OK')


if __name__ == '__main__':
    uvicorn.run(app='fastapi_demo:app')

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJPGView Question on Stackoverflow
Solution 1 - PythonJPGView Answer on Stackoverflow
Solution 2 - PythonJonhy BeebopView Answer on Stackoverflow
Solution 3 - PythonNicolas AcostaView Answer on Stackoverflow
Solution 4 - PythonSuyog ShimpiView Answer on Stackoverflow