Sub topic 6
Choosing version numbers: PEP 440 and semantic versioning
This chapter assumes you have a pyproject.toml with a [project] table and know how to build a package. It covers what a valid version string looks like, how to pick the next one, and how installers order versions.
Two standards, two jobs
PEP 440 is the Python standard for how a version string is written and compared. Semantic versioning (SemVer) is a convention for what the numbers mean. PyPI and pip enforce the first. Nothing enforces the second, so you must follow it yourself.
A SemVer-style version has three numbers, MAJOR.MINOR.PATCH:
| Part | Increase it when | Example |
|---|---|---|
| MAJOR | You make a change that breaks existing users | `1.4.2` to `2.0.0` |
| MINOR | You add a feature that stays compatible | `1.4.2` to `1.5.0` |
| PATCH | You fix a bug without changing the interface | `1.4.2` to `1.4.3` |
While the major number is 0, the API is not yet stable. Many projects allow breaking changes in minor releases during that phase. Say so in your README.
Where the version lives
The greeter project declares its version once, in pyproject.toml:
[project]
name = "greeter"
version = "0.1.0"The package then reads it from the installed metadata, so you never edit two files:
# src/greeter/__init__.py
from importlib.metadata import version
__version__ = version("greeter")Because the value comes from installed metadata, greeter.__version__ only works after the package is installed. An editable install (pip install -e .) is enough during development. After you change the version in pyproject.toml, reinstall so the metadata updates.
Pre-releases and other PEP 440 forms
PEP 440 allows suffixes that sort before the final release:
1.0.0a1is the first alpha.1.0.0b2is the second beta.1.0.0rc1is the first release candidate.1.0.0.dev3is a development release.1.0.0.post1is a post-release, used for a fix that does not touch the code, such as corrected metadata. It sorts after1.0.0.
The ordering is 1.0.0.dev3 < 1.0.0a1 < 1.0.0b2 < 1.0.0rc1 < 1.0.0 < 1.0.0.post1.
By default, pip install greeter skips pre-releases. Users get them only with pip install --pre greeter or by pinning the exact version. That makes a release candidate a safe way to test with real users.
You can check how Python parses and orders versions with the packaging library:
from packaging.version import Version
print(Version("1.0.0rc1") < Version("1.0.0"))
print(Version("1.0") == Version("1.0.0"))
print(Version("1.0.0-beta.1"))True
True
1.0.0b1PEP 440 normalizes spellings, so 1.0.0-beta.1 becomes 1.0.0b1. Trailing zeros do not matter. Pure SemVer forms such as 1.0.0-beta.1+build5 are not all valid, so write the PEP 440 form.
Common mistakes
- Reusing a version. PyPI never accepts the same filename twice, even after you delete a release. A broken
0.1.0means you ship0.1.1. - Hiding a breaking change in a patch release. Users who pin
greeter>=1.4,<2rely on your numbers. - Using an invalid string such as
v1.0or1.0-final. The build or upload fails.
Exercise
Your library is at 2.3.1. You add a new optional argument to an existing function, and old calls still work. Then you want to publish a release candidate for the next version. What are the final version and the release candidate string?
Show answer
A backward-compatible feature is a minor change, so the final version is 2.4.0. The first release candidate is 2.4.0rc1.