MrFaws Docs
Learn Python

Intermediate Python

Virtual environments, modules, packaging, typing, dataclasses and tests.

Virtual environments

python -m venv .venv
# Windows
.venv\Scripts\activate
# Linux and macOS
source .venv/bin/activate
python -m pip install --upgrade pip

Use one environment per project and record dependencies in pyproject.toml or a lock file.

Package structure

my-project/
  pyproject.toml
  src/my_app/__init__.py
  src/my_app/cli.py
  tests/test_cli.py

Keep I/O at the edges and business logic in importable functions. Avoid running work when a module is imported.

Type hints

from collections.abc import Iterable

def average(values: Iterable[float]) -> float:
    items = list(values)
    if not items:
        raise ValueError("values cannot be empty")
    return sum(items) / len(items)

Type hints improve editor support and document intent, but runtime validation is still needed at system boundaries.

Tests

Install pytest and write small tests for normal cases, boundaries and expected errors. Study PixelVault for a real package with modules and tests.

On this page