pyproject.toml: modern Python packaging (guide)

Learn pyproject.toml (PEP 621): venv, python -m build, wheels, console_scripts and salutcli mini-project. 20 exercises. Updated October 2026.

Short answer (October 2026): to make a Python project installable with pip (Python’s package installer), describe it in pyproject.toml following PEP 621 — a PEP (Python Enhancement Proposal, an official language change proposal). Build a wheel (.whl archive) with python -m build, then test with pip install -e . or pip install dist/….whl. This lesson sits on the advanced Python track after advanced typing and leads into the CLI journal packaging chapter (6/6).

Shipping a library or a command-line application means typing commands in a terminal (text window). CLI stands for Command Line Interface. You package code in a standard way: metadata, a clear src/ layout, a console_scripts entry (the shell command name after install, e.g. salutcli), and wheels for pip. The pyproject.toml file holds this configuration. A build backend (tool that reads pyproject.toml and builds the distribution) is declared under [build-system] — common choices are setuptools, hatchling, flit, and poetry; this guide uses setuptools.

This lesson is 100% Python packaging — no robot required. An editor and a venv with pip (isolated virtual environment) are enough.

Python terminal window showing pip and a version check

Screenshot (Emmanuel Sauvage, Robot Éducatif — October 2026): check Python and pip in a terminal, preferably with an active venv.

Info Detail
Goal Valid pyproject.toml, build a wheel, install and run a CLI command
Level Intermediate → advanced (exercises 15–20)
Time 45–90 minutes (mini-project + exercises)
Prerequisites venv and pip, Python basics, pathlib helps
Expected result A dist/ folder with .whl + a working salutcli command (mini-project)

1. Minimal pyproject.toml (concept)

Three blocks are enough to start:

[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"

[project]
name = "mytool"
version = "0.1.0"
dependencies = ["requests>=2.28"]

[project.scripts]
mytool = "mytool.cli:main"
  • [build-system] — which tool builds the wheel (here setuptools).
  • [project] — name, version and dependencies (visible on PyPI if you publish).
  • [project.scripts] — registers a shell command; mytool.cli:main points to main in cli.py.

2. Editable install

pip install -e . installs in development mode (-e = editable): source changes apply without reinstalling each time. The . is the folder that contains pyproject.toml.

Check: pip show mytool should list version and install path.

3. Wheel and sdist

python -m build creates two files under dist/:

  • wheel (.whl) — pre-built archive; fast pip install.
  • sdist (.tar.gz, source distribution) — source archive rebuilt on the user’s machine (slower).

For learning, a local wheel and pip install -e . are enough — no PyPI account required.

Pedagogical illustration: packaged Python project with pyproject.toml and installable wheel
Target outcome: metadata in pyproject.toml, artefacts in dist/ — Emmanuel Sauvage, Robot Éducatif, October 2026.

4. Versions and dependencies

Semantic versioning uses MAJOR.MINOR.PATCH (e.g. 1.2.3). In dependencies, ~=, >=, < are constraints for the pip resolver (engine that picks compatible versions).

Guided mini-project: salutcli command

Goal: an installable package that prints “Hello” with an optional name argument. About 30–40 minutes.

Folder layout (src/)

salutcli/
├── pyproject.toml
├── README.md
└── src/
    └── salutcli/
        ├── __init__.py
        └── cli.py

Files to create

src/salutcli/cli.py:

import sys

def main() -> None:
    name = sys.argv[1] if len(sys.argv) > 1 else "world"
    print(f"Hello, {name}!")

if __name__ == "__main__":
    main()

pyproject.toml (full example):

[build-system]
requires = ["setuptools>=61"]
build-backend = "setuptools.build_meta"

[project]
name = "salutcli"
version = "0.1.0"
requires-python = ">=3.10"
dependencies = []

[project.scripts]
salutcli = "salutcli.cli:main"

[tool.setuptools.packages.find]
where = ["src"]

Steps (run inside salutcli/)

  1. Create the tree and files above.
  2. Activate the venv, then pip install -e .
  3. Run salutcli Ada → should print Hello, Ada!
  4. Edit cli.py without reinstalling — editable mode picks up changes.
  5. python -m build (install build first if needed).
  6. Check dist/ for .whl and .tar.gz.
  7. (Optional) In a fresh venv: pip install dist/salutcli-0.1.0-py3-none-any.whl.

Check: pip show salutcli lists name, version and install location.

Troubleshooting

Symptom Fix
ModuleNotFoundError after pip install -e . Check where = ["src"] and package folder under src/; review venv and pip
salutcli not found Reactivate the venv; on Windows, open a new terminal after install
python -m build fails pip install build; validate TOML syntax
Wrong version installed Bump version in [project] or pip uninstall salutcli
Imports the wrong package from cwd Prefer src/ layout
ERROR: File "setup.py" not found Expected with pyproject.toml only — install build and run python -m build, not python setup.py
twine upload rejected Check version (not already on PyPI), TestPyPI vs production, and 2FA

FAQ

Do I need PyPI to learn this? No. Local wheels and pip install are enough. PyPI is for publishing to others.

Wheel vs sdist? A wheel installs as-is; an sdist is rebuilt from source on install.

Do I still need setup.py? For new projects, pyproject.toml only with setuptools ≥ 61 is standard.

External resources

Exercises (20)

Easy level

Exercise 1 — Name the two common artefact files produced by python -m build. Easy

Show the solution
.whl (wheel) and .tar.gz (sdist)

Exercise 2 — What does pip install -e . do? Easy

Show the solution
# Editable install: pip links to the current project's sources.
# Changes under src/ apply without repeating pip install.

Exercise 3 — Where do you declare a CLI foo pointing to pkg.cli:main? Easy

Show the solution
[project.scripts]
foo = "pkg.cli:main"

Exercise 4 — Difference between historical setuptools install_requires and [project] dependencies. Easy

Show the solution
# PEP 621 standardises metadata in [project].dependencies;
# setuptools maps them into wheel metadata (replaces legacy install_requires).

Exercise 5 — Why is a src/ layout recommended? Easy

Show the solution
# Avoids accidentally importing the wrong package from the current directory.

Exercise 6 — What is a MANIFEST.in file for? Easy

Show the solution
# Include non-Python files in the sdist (data, templates).

Exercise 7 — [project.optional-dependencies]: example dev with pytest. Easy

Show the solution
[project.optional-dependencies]
dev = ["pytest>=7"]

Intermediate level

Exercise 8 — pip install command with the dev extra. Intermediate

Show the solution
pip install ".[dev]"

Exercise 9 — Role of __version__ in __init__.py vs importlib.metadata.version("pkg"). Intermediate

Show the solution
# importlib.metadata reads the installed version from wheel metadata —
# source of truth after pip install (avoids drift from hard-coded __version__).

Exercise 10 — requires-python = ">=3.10" in [project]. Intermediate

Show the solution
requires-python = ">=3.10"

Exercise 11 — Why twine upload rather than pip for PyPI? Intermediate

Show the solution
# Dedicated upload tool with checks and secure HTTPS uploads to the index.

Exercise 12 — packages = ["pkg"] in setuptools vs automatic find:. Intermediate

Show the solution
# find discovers sub-packages; manual listing is fine for a simple structure.

Exercise 13 — Name a build backend other than setuptools. Intermediate

Show the solution
hatchling, flit, poetry-core, pdm-backend

Exercise 14 — LICENSE file and [project] license — legal importance. Intermediate

Show the solution
# Clarifies reuse rights; required for a serious publication.

Checkpoint before Hard level: you can write a minimal [project] table, declare a console_scripts entry, build with python -m build, and install editable with pip install -e .. If any step fails, redo the salutcli mini-project first.

Hard level

Exercise 15 — dynamic = ["version"] with setuptools-scm — idea. Hard

Show the solution
[project]
dynamic = ["version"]

[tool.setuptools_scm]

Version is derived from Git tags (e.g. v1.2.3 → 1.2.3).

Exercise 16 — A pip install conflict resolves two versions of the same package — how pip chooses (resolver concept). Hard

Show the solution
# Backtracking on constraints; error if the graph is impossible.

Exercise 17 — [tool.setuptools.package-data] for non-.py files. Hard

Show the solution
[tool.setuptools.package-data]
"pkg" = ["data/*.json"]

Exercise 18 — pip install --no-deps: risk. Hard

Show the solution
# Incomplete environment; runtime crash if dependencies are missing.

Exercise 19 — uv / pip-tools compile: interest for a pinned requirements.txt. Hard

Show the solution
# Reproducible resolution with hashes for deployments.

Exercise 20 — Namespace packages (pkg.ns) — one use case. Hard

Show the solution
# Plugins shipped separately under the same logical namespace (multiple wheels).

End of the advanced track

Guided project: CLI log analyser hub — apply this lesson in packaging chapter 6/6 (commands and file layout are language-independent).

Go further (Python on this site)

Amazon (affiliate)

Check today’s price on the listing. Partner link — commission possible on qualifying purchases, at no extra cost to you.

Emmanuel Sauvage — updated 31 October 2026.

Further reading

Was this page helpful?

Have a question about this guide or need advice? Ask us here

Torn between two models or stuck on a build? Drop us a line and we will get back to you by email.