6. Packaging & Tooling
Standardize builds and tooling in pyproject.toml; pin deps and automate quality checks.
Q1 What tools are essential for a modern Python project?
Answer: A modern Python project relies on tools for dependency management, code formatting, linting, and type checking, typically configured in pyproject.toml.
Explanation:
Dependency Management:
venvcreates isolated environments.pipinstalls packages. Arequirements.txt(frompip-tools) or apoetry.lockfile pins dependency versions for reproducible builds.Code Quality:
blackis the standard for formatting.ruffis an extremely fast linter and formatter that can replaceflake8andisort.Type Checking:
mypyis the standard for static type checking.Testing:
pytestis the de-facto standard for testing.
Additions: Centralize config in
pyproject.tomland usepre-commithooks to enforce formatting/linting before commits.
Q2 What belongs in pyproject.toml (PEP 621), and how do you pin deps?
Answer: Declare project metadata and tool configs in pyproject.toml; use pip-tools to compile locked requirements.txt; install dev code with pip install -e ..
[build-system]
requires = ["setuptools>=68", "wheel"]
build-backend = "setuptools.build_meta"
[project]
name = "myapp"
version = "0.1.0"
requires-python = ">=3.11"
dependencies = ["fastapi", "uvicorn"]