Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

PY
Python
0/14 completed
Beginner
Hello PythonVariables and typesWorking with stringsConditions and loopsFunctions
Intermediate
Lists, tuples, dicts and setsFiles and exceptionsModules and the standard library
Advanced
Classes and OOPIterators and generatorsDecorators and context managers
Pro
Async PythonTesting and type hintsVirtual environments, packages and real projects
Tracks / Python / Pro

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.

bash
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 environment

Your 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:

bash
pip freeze > requirements.txt       # save exact versions
pip install -r requirements.txt     # recreate on another machine

pyproject.toml and modern tools

Modern projects describe themselves in a single pyproject.toml file:

toml
[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` command

uv has become a very popular all-in-one tool. It's extremely fast, and replaces pip, venv and more:

bash
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 pytest

Poetry 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.py

A command-line tool

argparse (built in) turns a script into a proper tool with options and --help:

Python · editable

Logging instead of print

Python · editable

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:

python
import os

API_KEY = os.environ["WEATHER_API_KEY"]               # fails loudly if missing
DEBUG = os.environ.get("DEBUG", "false") == "true"    # optional, with a default

During development, keep them in a .env file (git-ignored) and load it with python-dotenv.

Code quality tools

bash
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 tests

Run 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:

  1. A Product dataclass (name, url, target_price, history of prices with dates).
  2. Store tracked products in a JSON file (or sqlite3), via a storage.py module.
  3. A CLI with subcommands: add, list, check, remove (argparse subparsers).
  4. check fetches each product's price concurrently with httpx.AsyncClient and asyncio.gather (start with a fake async function that returns random prices).
  5. Print a table with rich, highlighting products below their target price.
  6. Logging, configuration from environment variables, and helpful errors (no tracebacks for user mistakes).
  7. pytest tests for storage and price parsing; mypy and ruff passing; a GitHub Actions workflow running them.
✎ Check your understanding

1.Why use a virtual environment?

2.What should you commit to git?

3.Where should an API key come from in production code?

← Testing and type hints