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?
FastAPI is a Python framework and set of tools that enables developers to use a REST interface to call commonly used functions to implement applications. It is accessed through a REST API to call common building blocks for an app. In this example, the author uses FastAPI to create accounts, login, and authenticate.
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)
Normally, you'll start the server by running the following command,
python main.py
For this setup, and now, you can set the script path in Pycharm's config

uvicorn commandIn 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"}
Normally, you'll start the server by running the following command,
uvicorn main:app --reload
For this setup, and now, you can set the script path in Pycharm's config

uvicorn
uvicorn binary. You will get the path by executing the command, which uvicorn , inside your environment. (See this image)uvicorn commandYou can do it without adding code to main.py
target to run instead of Script path choose Module name
Module name type uvicorn
app.main:app --reload --port 5000

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With