Sub topic 6
Choosing version numbers: PEP 440 and semantic versioning
This chapter assumes you can read a pyproject.toml file, have built a wheel with python -m build, and know what pip install does. You will learn which version strings PyPI accepts, how pip orders them, and how to pick a number that tells users how big a change is.
Two systems that answer different questions
Two documents shape how Python libraries are versioned, and they do different jobs.
- PEP 440 is the Python packaging standard for version syntax. It defines which strings are valid and how tools compare them. It says nothing about what a number means.
- Semantic versioning (SemVer) is a convention for meaning. It says which part of the number you raise for which kind of change.
You use both. PEP 440 decides whether PyPI and pip can read your version. SemVer helps your users decide whether upgrading is safe.
The PEP 440 grammar
A full version string has these parts, and only the release segment is required:
[N!]N(.N)*[{a|b|rc}N][.postN][.devN][+local]| Part | Example | Meaning |
|---|---|---|
| Epoch | `1!2.0` | Overrides ordering after a scheme change. Rare. |
| Release | `1.4.2` | One or more dot-separated integers. |
| Pre-release | `1.4.2a1`, `1.4.2b2`, `1.4.2rc1` | Alpha, beta, or release candidate. |
| Post-release | `1.4.2.post1` | A fix that does not change the code, such as packaging metadata. |
| Dev release | `1.4.2.dev3` | A snapshot taken before a pre-release. |
| Local version | `1.4.2+ubuntu1` | Downstream build label. PyPI rejects it. |
Tools normalize some spellings. 1.0-alpha.1, 1.0alpha1, and 1.0a1 all mean the same version, and pip treats 1.0.0 and 1.0 as equal. Write the normalized form (1.0a1) in your own files so that your filenames and metadata match what other tools show.
How versions are ordered
pip picks the highest version that satisfies a requirement, so the ordering rules decide what users get. The order from lowest to highest is:
1.0.dev11.0a11.0b11.0rc11.01.0.post11.1.dev1
Two consequences matter in practice.
First, pip skips pre-releases and dev releases by default. A user running pip install greeter gets 1.0, not 1.1rc1, even though 1.1rc1 is numerically higher. They must pass --pre or pin the exact version. This makes release candidates safe to publish on PyPI.
Second, you can check any ordering yourself with the packaging library, which pip uses internally:
from packaging.version import Version, InvalidVersion
versions = ["1.0", "1.0rc1", "1.0.post1", "1.0.dev1", "1.0a1", "1.0b1"]
for v in sorted(versions, key=Version):
print(v)
print(Version("1.0.0") == Version("1.0"))
print(Version("1.0rc1").is_prerelease)
try:
Version("v1.0-final")
except InvalidVersion as exc:
print("invalid:", exc)Run it with python -m pip install packaging first. The sorted output matches the list above, 1.0.0 == 1.0 prints True, and the last version is rejected.
SemVer: what each number promises
SemVer uses three release integers, MAJOR.MINOR.PATCH:
| Raise | When | Example |
|---|---|---|
| MAJOR | You make an incompatible change to the public API. | `1.4.2` to `2.0.0` |
| MINOR | You add functionality in a backward-compatible way. | `1.4.2` to `1.5.0` |
| PATCH | You fix a bug without changing the API. | `1.4.2` to `1.4.3` |
When you raise a number, reset the numbers to its right to zero.
SemVer only works if you know what your public API is. For a library like greeter, that is greeter.greet, the greeter command, and anything you document. Names that start with an underscore, and modules you did not mention in the docs, are not part of the promise. Removing a function argument, changing a return type, or raising a new exception from existing code are all breaking changes, even when the code still imports.
Versions below 1.0
SemVer treats 0.y.z as unstable: anything may change at any time. Many projects still use the minor number as the breaking-change signal in this range, so 0.2.0 to 0.3.0 may break things while 0.2.0 to 0.2.1 should not. Say which rule you follow in your README. Moving to 1.0.0 is a statement that the public API is stable enough to promise compatibility.
Where SemVer and PEP 440 disagree
SemVer writes pre-releases as 1.0.0-rc.1. PEP 440 writes 1.0.0rc1. Use the PEP 440 form in your package, since that is what PyPI validates. Also note that PEP 440 allows four or more release segments (1.2.3.4) and calendar-based versions (2026.11). Those are valid, but they do not carry SemVer meaning. Projects that release on a schedule, rather than by compatibility, sometimes pick calendar versioning on purpose.
Where the version lives
In the example project, the version is written in exactly one place, the [project] table:
[project]
name = "greeter"
version = "0.1.0"The package then reads it from the installed metadata:
# src/greeter/__init__.py
from importlib.metadata import version
__version__ = version("greeter")This avoids the common bug where pyproject.toml says 0.2.0 but __version__ still says 0.1.0. The cost is that importlib.metadata only finds the version after the package is installed. In a src layout you install in editable mode (pip install -e .) during development, so this is normally fine. If you edit the version, reinstall, because editable installs keep the old metadata until you do.
Two alternatives exist. You can mark the version as dynamic and let the backend read it from a file:
[project]
name = "greeter"
dynamic = ["version"]
[tool.setuptools.dynamic]
version = {attr = "greeter.__version__"}Or you can derive it from Git tags with a plugin such as setuptools-scm. Tag-derived versions remove manual edits, but your build now depends on Git history being present, which breaks shallow clones and builds from a plain source archive unless you configure around it.
Rules that PyPI enforces
- A version can be uploaded once. PyPI refuses a second upload of the same filename, even after you delete the release. A mistake always costs you a new version number.
- Local versions are rejected.
1.0+customfails at upload. - The version must be valid PEP 440.
twine checkand the upload both validate it. - Filenames embed the version.
greeter-0.1.0-py3-none-any.whlcarries the normalized version, so a mismatch between the tag and the metadata shows up here.
Because uploads are permanent, use pre-releases to test publicly. Publish 0.2.0rc1, let a few users try it with pip install --pre greeter, and release 0.2.0 when it holds up. For quick private iteration, use TestPyPI, which the later chapter on rehearsals covers.
Choosing the number: a decision guide
Some cases that cause arguments:
- Dropping an old Python version. Raising
requires-pythonstops some users from installing the new release. Many projects treat this as a minor change, since pip falls back to the older release for those users. State your policy in the changelog. - Raising a dependency's minimum version. This can force users to upgrade other packages. Treat it as at least a minor change.
- Deprecating a function. Emitting a
DeprecationWarningis a minor change. Removing the function later is a major change. - A bug fix that changes behavior people relied on. If the old behavior was documented, the fix is breaking. If it was an accident, call it a patch and mention it in the changelog.
- A packaging-only mistake, such as a missing license file. If the code is identical,
1.0.post1is valid. Most projects simply release1.0.1, which is easier for users to understand.
Common errors
- Using
vprefixes in metadata. Git tags likev1.0.0are fine, and PEP 440 tolerates a leadingvwhen it normalizes. Still, write1.0.0inpyproject.toml. - Reusing a version after a failed release. If any file of that version reached PyPI, you cannot replace it. Bump the patch number.
- Publishing
1.0.0-beta. Use1.0.0b1. The hyphenated form normalizes, but you should not rely on that. - Expecting
--prebehavior from a range. A requirement such asgreeter>=1.0still skips1.1rc1unless the user opts in, or unless the specifier itself names a pre-release. - Forgetting to reinstall after a version edit and then wondering why
__version__is stale.
Exercise
Order these versions from lowest to highest, then say which one pip install greeter selects when all of them are on PyPI and the user adds no flags: 1.2.0, 1.2.0rc1, 1.2.1.dev2, 1.2.0.post1, 1.3.0b1.
Show answer
Lowest to highest: 1.2.0rc1, 1.2.0, 1.2.0.post1, 1.2.1.dev2, 1.3.0b1. By default pip ignores pre-releases and dev releases, which removes 1.2.0rc1, 1.2.1.dev2, and 1.3.0b1. Among the remaining final and post releases, the highest is 1.2.0.post1, so pip installs that.
Exercise
Version 2.3.1 of your library has a documented function greet(name). For each change, give the next version: (a) you add an optional greeting argument with a default that keeps old output, (b) you fix a crash on empty strings, (c) you rename greet to say_hello with no alias.
Show answer
(a) 2.4.0, a backward-compatible feature, so MINOR is raised and PATCH resets. (b) 2.3.2, a bug fix, so PATCH is raised. (c) 3.0.0, since removing a documented public name breaks callers, so MAJOR is raised and the others reset. Keeping greet as a deprecated alias would make the same change a minor release, 2.4.0.