If you’ve tried to start your API and got Could not import module "main", the tool is fine and the target is wrong. You run FastAPI with Uvicorn by opening a terminal in the folder that holds main.py and typing uvicorn main:app.
That one string trips people up because it looks like a file path, and it isn’t. In this tutorial, I’ll explain what it means and walk through six ways to start the server, from a plain command to uvicorn.run() inside Python.
I ran every command on Windows 11 with Python 3.12.5, FastAPI 0.162.2 and Uvicorn 0.54.0, then broke things on purpose. The port-conflict results surprised me, so that section shows what really happened.
The sample National Parks API we’ll use
Everything below uses one small file. If FastAPI isn’t on your machine yet, install FastAPI and create your first API first; pip install "fastapi[standard]" brings Uvicorn along with it.
Save this as main.py in an empty folder. I’ll call the folder parks:
import os
from fastapi import FastAPI
app = FastAPI(title="National Parks API")
PARKS = [
{"code": "yell", "name": "Yellowstone", "state": "WY", "entry_usd": 35.00},
{"code": "grca", "name": "Grand Canyon", "state": "AZ", "entry_usd": 35.00},
{"code": "acad", "name": "Acadia", "state": "ME", "entry_usd": 35.00},
{"code": "grsm", "name": "Great Smoky Mountains", "state": "TN", "entry_usd": 0.00},
]
@app.get("/parks")
def list_parks(state: str | None = None):
if state:
return [p for p in PARKS if p["state"] == state.upper()]
return PARKS
@app.get("/worker")
def worker():
return {"pid": os.getpid()}
It lists four parks, and three details matter later. Great Smoky Mountains costs $0.00, the state filter accepts lowercase, and /worker returns the process ID so we can see which process answered.
PARKS is uppercase by the usual naming habit for constants in Python, since nothing here changes it.
What does main:app mean in Uvicorn?
Uvicorn is the ASGI server, the program that listens on a port and hands each request to your app. It needs an import string, written as module:attribute, to find that app.
| Part | In our command | Meaning |
|---|---|---|
| Module | main | The Python module main, which is main.py without the extension |
| Colon | : | Separates the module from the object inside it |
| Attribute | app | The variable holding FastAPI() |
So uvicorn main:app does the same job as from main import app. Inside a package you use dots for the module, such as uvicorn app.main:app, and this FastAPI project structure guide shows that layout.
Method 1: Run FastAPI with the uvicorn command (Recommended)
Use this when you want the plain server with no extras. If you built a first app in the FastAPI tutorial for beginners, the same command starts it. Open Command Prompt in the parks folder and run this:
uvicorn main:app
INFO: Started server process [4208]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
The server is up on port 8000. Calling /parks?state=wy returned exactly one park:
[{"code":"yell","name":"Yellowstone","state":"WY","entry_usd":35.0}]

Application startup complete means your app imported and is serving.How does this work?
Uvicorn imports main, grabs app, runs startup and binds to 127.0.0.1:8000. Those are the defaults, so you don’t have to type them.
The lowercase wy still matched because the route calls state.upper(). Press Ctrl+C to stop the server.
Same server, different view: open http://127.0.0.1:8000/docs for Swagger UI. Expanding GET /parks, choosing Try it out and sending WY returned Yellowstone again:

/docs under Uvicorn too.Method 2: Restart Uvicorn automatically with –reload
Use this while you’re writing code. Without it you’d stop and restart the server after every save. Here’s the command on the same parks API:
uvicorn main:app --reload
I changed "Acadia" to "Acadia National Park" and saved. The next call to /parks?state=me returned the new name without my touching the terminal, and the log showed this:
WARNING: WatchFiles detected changes in 'main.py'. Reloading...
INFO: Shutting down
INFO: Waiting for application shutdown.
INFO: Application shutdown complete.
INFO: Waiting for application startup.
INFO: Application startup complete.

How does this work?
A small parent process, the reloader, watches your folder. When a .py file changes, it stops the worker process and starts a new one, so your code is imported from scratch.
That watcher is WatchFiles, which comes with uvicorn[standard]. If another machine behaves differently, pin the same package versions there.
Important: the FastAPI docs say not to use --reload in production, since it uses far more resources and is less stable.
Method 3: Set the Uvicorn host and port
Use --port when 8000 is taken, and --host when another machine has to reach the API. Same file again:
uvicorn main:app --host 0.0.0.0 --port 8010

netstat showed the server listening on 0.0.0.0:8010, compared with 127.0.0.1:8000 by default. Requesting Maine through http://127.0.0.1:8010 returned Acadia at $35.00.
How does this work?
127.0.0.1 means this computer only. 0.0.0.0 means every network interface, so other devices can connect if your firewall allows it.
Don’t paste 0.0.0.0 into your browser, though; browse to 127.0.0.1 or the machine’s real IP. On a server I usually keep the port in an environment variable instead of hard-coding it.
Method 4: Run several Uvicorn workers
Use --workers when one process can’t keep up and the machine has spare CPU cores. Each worker is a separate process running its own copy of the parks API:
uvicorn main:app --workers 3
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started parent process [5588]
INFO: Started server process [57896]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [60848]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Started server process [26500]
INFO: Waiting for application startup.
INFO: Application startup complete.

I then called /worker 40 times, and the answers came from 3 different process IDs.
How does this work?
The parent owns the socket and watches the children, while the operating system spreads connections among them. The split isn’t even, and memory isn’t shared, so a mutable Python list changed in one worker stays unchanged in the others.
Important: --reload and --workers don’t combine. Uvicorn printed "workers" flag is ignored when reloading is enabled. and ran one process. On Kubernetes, the FastAPI docs suggest one process per container instead.
Method 5: fastapi dev vs fastapi run vs uvicorn
The fastapi command is a friendlier front end, and it still uses Uvicorn underneath. You give it a file path instead of an import string, and it works out main:app for you:
fastapi dev main.py
fastapi run main.py
