Python - Tooling & Dev Experience

Overview

Estimated time: 30–45 minutes

Set up a productive Python workflow: editors, formatters, linters, tests, and environment management.

Learning Objectives

  • Use black and ruff for fast, consistent formatting and linting.
  • Run tests quickly with pytest and manage environments with venv/pyenv/pipx.
  • Integrate tooling via pre-commit hooks and editor extensions.

Prerequisites

Format & Lint

pip install black ruff
black .
ruff .

Tests

pip install pytest
pytest -q

pre-commit

pip install pre-commit
pre-commit sample-config > .pre-commit-config.yaml
pre-commit install

Environment basics

python -m venv .venv
source .venv/bin/activate  # Windows: .venv\\Scripts\\activate
pip install -U pip

Common Pitfalls

  • Global installs causing version conflicts—use virtual environments.
  • Running tools inconsistently—automate with pre-commit and CI.

Checks for Understanding

  1. Why run formatters/linters locally and in CI?
  2. What’s the benefit of pre-commit over manual tool runs?
Show answers
  1. Catch issues early and ensure consistent results across machines.
  2. Automates checks on each commit to prevent regressions slipping in.

Exercises

  1. Add black and ruff to a project and configure them in .pre-commit-config.yaml.
  2. Set up a fresh venv and run pytest; add one failing and one passing test.