Virtual environments and packages (before the web modules)
By this point in the course you have been writing Python with whatever interpreter your template and editor use. For the final web chapters, you want an isolated environment and the right libraries installed cleanly.
Virtual environments
A virtual environment is a directory of packages for one project.
python3 -m venv .venv
Activate:
- macOS / Linux:
source .venv/bin/activate - Windows (cmd):
.venv\Scripts\activate.bat - Windows (PowerShell):
.venv\Scripts\Activate.ps1
Deactivate: deactivate
Tip
After activating, run
python --versionandwhich python(orwhere python) to confirm you are inside the venv.
Installing web and HTTP dependencies
Your Classroom template may ship pyproject.toml or requirements.txt. Typical packages for the closing chapters:
fastapi— web frameworkuvicorn— ASGI serverpydantic— data validation (you already used it earlier; ensure it is installed here if needed)httpx— HTTP client
Example:
pip install fastapi uvicorn pydantic httpx
Or use pip install -r requirements.txt, uv sync, or Poetry, per your template.
Running the web server (preview)
Once app exists in a module (next chapters):
uvicorn app:app --host 0.0.0.0 --port 8000 --reload
First app = module name; second app = FastAPI() instance.
Implement
- Create and activate
.venvin your repo (if you have not already). - Install FastAPI, Uvicorn, HTTPX, and ensure Pydantic is available.
- Confirm
python -c "import fastapi, uvicorn, httpx, pydantic"succeeds. - Commit lockfiles / dependency files your instructor wants—not
.venvitself.