Sub topic 2
Choosing Versions and Verifying Python Distribution Files
Assume you can import Python packages, use virtual environments and pip, and run basic shell commands. This chapter connects versioning with the files that users install.
Choosing a version number
PEP 440 defines version formats that Python packaging tools can compare. Semantic versioning uses MAJOR.MINOR.PATCH to communicate compatibility:
- Increase
MAJORfor incompatible changes. - Increase
MINORfor backward-compatible features. - Increase
PATCHfor backward-compatible fixes.
For example, 1.4.2 is a normal PEP 440 version. PEP 440 also supports prereleases such as 2.0.0rc1 and development releases such as 2.0.0.dev1. Choose one policy and update the version before building. A release cannot reuse the same version on PyPI.
Exposing a command-line tool
An entry point connects a command name to a Python function:
[project.scripts]
greeter = "greeter.cli:main"After installation, running greeter Ada calls greeter.cli.main(). Keep the version in one place, usually pyproject.toml, instead of duplicating it in several modules.
Including files and type information
License files belong in the source distribution and should be declared explicitly:
[project]
license = "MIT"
license-files = ["LICENSE"]
[tool.setuptools.package-data]
greeter = ["py.typed", "data/*.json"]The py.typed marker tells type checkers that the installed package includes supported type information. Package data must be inside the package and configured so the build backend copies it into the wheel.
Building the sdist and wheel
A source distribution, or sdist, contains source files and build metadata. A wheel is a ready-to-install archive. Build both from a clean tree:
python -m pip install build
python -m buildThe command creates files in dist/, such as greeter-1.4.2.tar.gz and greeter-1.4.2-py3-none-any.whl.
Inspecting before upload
Check metadata before publishing:
python -m pip install twine
python -m twine check dist/*
python -m zipfile -l dist/greeter-1.4.2-py3-none-any.whlConfirm the name, version, license, package data, and py.typed marker. Then install the wheel in a fresh virtual environment and run its tests.
Exercise
Which artifact should you install to verify that package data is present after installation?
Show answer
Install the wheel in a fresh virtual environment. It represents the files users receive from a normal pip install.