Packaging & venv — Cheat Sheet
Python A–Z · 2 topics. Download the PDF or the Instagram carousel and share it.
Cheat Sheet · AiCanCode.org
Packaging & venv
Python A–Z2 topicsQuick revision reference
1
Virtual Environments & pip — Isolating Every Project
python -m venv .venv gives each project its own interpreter and packages; pip installs into it; requirements.txt with pinned versions makes the setup reproducible on any machine.
- ✓One venv per project; global installs are how projects break each other
- ✓Activate first — then python/pip mean the project-local copies
- ✓pip freeze > requirements.txt pins exact versions; commit the file, ignore .venv/
- ✓Same requirements.txt drives teammates, CI, and Docker builds; uv is the fast modern drop-in
create → activate → install → work → deactivate
# Create (once per project, inside the project folder) python -m venv .venv # Activate (every new terminal session) .venv\Scripts\activate # Windows source .venv/bin/activate # macOS / Linux # prompt becomes: (.venv) C:\projects\campus-api> # Install packages — they go into .venv only pip install requests "fastapi[standard]" sqlalchemy pip list # what is installed here pip show requests # version, dependencies, location # Leave the environment deactivate # .gitignore MUST contain: # .venv/
2
Packaging with pyproject.toml — From Script to Installable Package
pyproject.toml is the single modern config for a Python package: metadata, dependencies, and console scripts in one file — pip install -e . for development, python -m build + twine to publish.
- ✓pyproject.toml is the modern standard — setup.py is legacy
- ✓src/ layout prevents "works locally, breaks installed" import bugs
- ✓pip install -e . = editable mode: edits apply without reinstalling
- ✓[project.scripts] creates console commands; build + twine publish to PyPI
One file replaces setup.py, setup.cfg and MANIFEST.in
# Project layout (src/ layout — the recommended one)
# campus-utils/
# ├── pyproject.toml
# ├── README.md
# └── src/
# └── campus_utils/
# ├── __init__.py
# ├── cli.py
# └── marks.py
# ── pyproject.toml ──────────────────────────────
[project]
name = "campus-utils"
version = "0.1.0"
description = "Marksheet and placement helpers"
requires-python = ">=3.10"
dependencies = [
"requests>=2.31",
"pydantic>=2.0",
]
[project.optional-dependencies]
dev = ["pytest", "ruff"]
[project.scripts]
campus = "campus_utils.cli:main" # installs a real 'campus' command
[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"Learn this free with Aria, your AI tutor → AiCanCode.org/learn/python