Virtual environments, packages and real projects
You can write Python. This last lesson covers how real Python projects are set up, shared and shipped: virtual environments, dependencies, project structure, and what to build next.
Virtual environments
Each project needs its own set of packages, sometimes in different versions: project A uses Django 4, project B uses Django 5. Installing everything globally ends in conflicts. A virtual environment (venv) is a private folder of packages for one project.
cd my-project
python3 -m venv .venv # create it (once)
source .venv/bin/activate # activate (macOS/Linux)
.venv\Scripts\activate # activate (Windows)
pip install requests rich # installs into .venv only
pip list
deactivate # leave the environmentYour prompt shows (.venv) while it's active. Add .venv/ to .gitignore: you share the list of packages, not the packages themselves.
Recording dependencies
The classic way is a requirements.txt:
pip freeze > requirements.txt # save exact versions
pip install -r requirements.txt # recreate on another machinepyproject.toml and modern tools
Modern projects describe themselves in a single pyproject.toml file:
[project]
name = "price-tracker"
version = "0.1.0"
requires-python = ">=3.12"
dependencies = [
"httpx>=0.27",
"rich>=13",
]
[project.optional-dependencies]
dev = ["pytest", "ruff", "mypy"]
[project.scripts]
price-tracker = "price_tracker.cli:main" # creates a `price-tracker` commanduv has become a very popular all-in-one tool. It's extremely fast, and replaces pip, venv and more:
uv init price-tracker # new project with pyproject.toml
cd price-tracker
uv add httpx rich # add dependencies (creates .venv and a lock file)
uv add --dev pytest ruff
uv run python -m price_tracker # run inside the project's environment
uv run pytestPoetry and PDM are popular alternatives. Whichever you choose, commit the lock file (uv.lock, poetry.lock) so everyone gets identical versions.
Project structure
A clean layout for an application or library:
price-tracker/
├── pyproject.toml
├── README.md
├── .gitignore
├── src/
│ └── price_tracker/
│ ├── __init__.py
│ ├── __main__.py # lets you run: python -m price_tracker
│ ├── cli.py # command-line interface
│ ├── scraper.py # one module per responsibility
│ └── storage.py
└── tests/
├── test_scraper.py
└── test_storage.pyA command-line tool
argparse (built in) turns a script into a proper tool with options and --help:
Logging instead of print
Logs have levels, timestamps and sources, and can go to files or monitoring systems without changing your code.
Configuration and secrets
Never hard-code passwords or API keys. Read them from environment variables:
import os
API_KEY = os.environ["WEATHER_API_KEY"] # fails loudly if missing
DEBUG = os.environ.get("DEBUG", "false") == "true" # optional, with a defaultDuring development, keep them in a .env file (git-ignored) and load it with python-dotenv.
Code quality tools
uv add --dev ruff mypy pytest
ruff check . # lint: find bugs and bad patterns
ruff format . # format the code consistently
mypy src # type check
pytest # run the testsRun these automatically: with pre-commit hooks before each commit, and in CI (such as GitHub Actions) on every pull request.
Where Python takes you next
| Direction | Learn next |
|---|---|
| Web backends and APIs | FastAPI or Django (see the Django & FastAPI track), SQL, PostgreSQL |
| Data analysis | pandas, Jupyter, matplotlib/seaborn, SQL |
| Machine learning and AI | numpy, scikit-learn, PyTorch, working with LLM APIs |
| Automation and DevOps | Scripting with pathlib/subprocess, Ansible, cloud SDKs (boto3) |
| Scraping | httpx + BeautifulSoup, Playwright |
🎓 Python track complete!
You know Python's core (types, strings, control flow, functions, collections), files and exceptions, modules, OOP and dataclasses, iterators and generators, decorators and context managers, async, testing and type hints, and how real projects are organised.
Capstone project: a price-tracker CLI
Build it on your computer with uv:
- A
Productdataclass (name, url, target_price, history of prices with dates). - Store tracked products in a JSON file (or
sqlite3), via astorage.pymodule. - A CLI with subcommands:
add,list,check,remove(argparse subparsers). checkfetches each product's price concurrently withhttpx.AsyncClientandasyncio.gather(start with a fake async function that returns random prices).- Print a table with
rich, highlighting products below their target price. - Logging, configuration from environment variables, and helpful errors (no tracebacks for user mistakes).
- pytest tests for storage and price parsing; mypy and ruff passing; a GitHub Actions workflow running them.
1.Why use a virtual environment?
2.What should you commit to git?
3.Where should an API key come from in production code?