Back to list
FastAPIPythonvenvuvicornSetup

[FastAPI][Setup] Create a Python venv and Scaffold FastAPI

Stop installing FastAPI globally - use venv per project, install fastapi and uvicorn, write a minimal main.py, and verify with uvicorn --reload.

1. Environment

  • OS: Windows 11 / macOS
  • Language: Python 3.10+
  • IDE: Visual Studio Code (VS Code)
  • Terminal: PowerShell (Windows) / zsh (macOS)

2. Problem

A common first mistake for new FastAPI backends is running pip install fastapi directly in the global terminal.

As projects pile up, you eventually hit the classic failure: *“The server that worked yesterday suddenly does not.|–

3. Cause

The root issue is global package installs.

Without a virtual environment, pip install writes into one shared system space. If project A needs FastAPI 0.68 and you globally install 0.100 for project B, A can break from version conflicts.

Give each project its own isolated space (venv) to prevent that.

4. Solution

1) Create and activate a venv

Standard flow: use Python?’s built-in venv, then install FastAPI inside it.

mkdir fastapi-backend
cd fastapi-backend

# Create the virtual environment
python -m venv .venv

# Activate (Windows)
.venv\Scripts\activate

# Activate (macOS / Linux)
source .venv/bin/activate

When the prompt shows (.venv), activation succeeded. From then on, installs stay isolated inside that folder.

2) Install FastAPI and Uvicorn

  • FastAPI (framework): Define routes and responses in Python; also generates API docs (Swagger).
  • Uvicorn (ASGI server): The engine that accepts HTTP and runs your FastAPI app. The framework cannot serve itself alone - similar in role to a lightweight Nginx for ASGI apps.
pip install fastapi "uvicorn[standard]"

Create main.py at the project root:

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI!"}

3) Run and verify

uvicorn main:app --reload

Open http://127.0.0.1:8000 and confirm the JSON message appears.

Next step for React: CORS middleware and modular routers

5. Summary

For Python backends, a stable toolchain is venv + pip + FastAPI + Uvicorn. Using an isolated environment per project is what keeps versions from colliding and servers from “mysteriously|–breaking overnight.

Related posts