PEP 440 Versions & Specifiers
Every tool in this course compares versions: pip picking the newest match, Poetry turning ^2.31 into a range, uv locking, PyPI rejecting an upload. They all follow PEP 440 (now the "Version specifiers" specification on packaging.python.org). It's close to Semantic Versioning but not the same, and the differences bite.
What a version looks like
[N!]N(.N)*[{a|b|rc}N][.postN][.devN][+local]
1! 2.0.1 rc1 .post2 .dev3 +ubuntu1| Part | Example | Meaning |
|---|---|---|
| epoch | 1!2.0 |
rarely used: resets ordering after a change of scheme (1!1.0 > 2024.10) |
| release | 2.31.0, 2026.10.23 |
any number of numeric parts; 2.31 == 2.31.0 |
| pre-release | 2.0a1, 2.0b2, 2.0rc1 |
alpha < beta < release candidate < final |
| post-release | 2.0.post1 |
a fix to the packaging of 2.0 (docs, metadata), sorts after 2.0 |
| dev-release | 2.0.dev3 |
a development snapshot, sorts before everything else of 2.0 |
| local | 2.0+cpu, 1.0+ubuntu1 |
a downstream build; not allowed on PyPI |
Versions are normalised: 1.0-RC1, 1.0rc1 and 1.0.rc.1 are the same version, written 1.0rc1. And they're compared numerically part by part, never as strings: 2.10 > 2.9.
The full ordering for one release is:
2.0.dev1 < 2.0a1.dev1 < 2.0a1 < 2.0b1 < 2.0rc1 < 2.0 < 2.0.post1 < 2.0.1Specifiers
| Operator | Example | Matches |
|---|---|---|
== |
==2.31.0 |
exactly 2.31.0 (and 2.31 too, by padding) |
== with * |
==2.* |
any 2.x |
!= |
!=2.32.0 |
anything except that one (bad release) |
>=, <=, >, < |
>=2.30,<3 |
ranges; the comma means AND |
~= |
~=2.31 |
"compatible release": >=2.31, ==2.* |
~= |
~=2.31.0 |
>=2.31.0, ==2.31.*: patch updates only |
=== |
===foobar |
string equality, for non-PEP 440 versions (avoid) |
Two rules people forget:
- Pre-releases are excluded unless a specifier names one (
>=2.0rc1) or you ask for them (pip install --pre, uv--prerelease allow). If the only matching version is a pre-release, pip will use it. - There's no caret (
^) or tilde (~) in PEP 440. Those are Poetry's syntax; Poetry translates^2.31into>=2.31,<3.0.0when it writes standard metadata.
Trying specifiers against a real index
The local index has requests 2.31.0 and 2.34.2. pip install --dry-run resolves without installing, so it's a cheap way to test what a specifier selects:
python3 -m venv .venv && . .venv/bin/activate
for spec in "requests" "requests~=2.31.0" "requests~=2.31" "requests==2.*" \
"requests!=2.34.2" "requests>=2.32,<2.34" ; do
printf '%-22s -> ' "$spec"
pip install --dry-run --no-deps "$spec" 2>&1 | grep -E '^Would install|^ERROR: No matching' | sed 's/ERROR: //'
donerequests -> Would install requests-2.34.2
requests~=2.31.0 -> Would install requests-2.31.0
requests~=2.31 -> Would install requests-2.34.2
requests==2.* -> Would install requests-2.34.2
requests!=2.34.2 -> Would install requests-2.31.0
requests>=2.32,<2.34 -> No matching distribution found for requests<2.34,>=2.32
A runnable model: comparing versions with packaging
packaging is the library pip, uv's Python tooling, Poetry, and build backends use for this, and it's available in this site's in-browser Python:
from packaging.version import Version, InvalidVersion
from packaging.specifiers import SpecifierSet
versions = ["2.0", "2.0.post1", "2.0rc1", "2.0.dev1", "2.0a1", "2.0.1", "1!0.1", "2.0b1", "2.10", "2.9"]
print(" < ".join(str(v) for v in sorted(Version(v) for v in versions)))
print(Version("1.0-RC1"), Version("1.0.rc.1"), Version("1.0rc1") == Version("1.0-RC1"))
for bad in ["1.0-beta-final", "latest"]:
try:
Version(bad)
except InvalidVersion as e:
print("invalid:", e)
available = ["2.29.0", "2.31.0", "2.32.0", "2.33.0rc1", "2.34.2", "3.0.0"]
for spec in ["~=2.31", "~=2.31.0", ">=2.30,!=2.32.0,<3", ">=2.33.0rc1,<3", "==2.*"]:
s = SpecifierSet(spec)
print(f"{spec:<20} {list(s.filter(available))}")2.0.dev1 < 2.0a1 < 2.0b1 < 2.0rc1 < 2.0 < 2.0.post1 < 2.0.1 < 2.9 < 2.10 < 1!0.1
1.0rc1 1.0rc1 True
invalid: Invalid version: '1.0-beta-final'
invalid: Invalid version: 'latest'
~=2.31 ['2.31.0', '2.32.0', '2.34.2']
~=2.31.0 ['2.31.0']
>=2.30,!=2.32.0,<3 ['2.31.0', '2.34.2']
>=2.33.0rc1,<3 ['2.33.0rc1', '2.34.2']
==2.* ['2.29.0', '2.31.0', '2.32.0', '2.34.2']
The fourth specifier names a pre-release, so 2.33.0rc1 becomes eligible.
A runnable model: a hand-written comparator
To see that there's no magic, here is a cut-down PEP 440 comparator: release numbers padded with zeros, then a pre/final/post phase, then the dev marker. It skips epochs and local versions, but agrees with packaging on everything above:
import re
PATTERN = re.compile(r"^(\d+(?:\.\d+)*)(?:[-_.]?(a|b|rc)(\d+))?(?:\.post(\d+))?(?:\.dev(\d+))?quot;)
def key(v):
m = PATTERN.match(v.lower())
if not m:
raise ValueError(f"not a version: {v}")
release, pre_l, pre_n, post, dev = m.groups()
nums = [int(x) for x in release.split(".")]
while nums and nums[-1] == 0: # 2.0 == 2.0.0
nums.pop()
if pre_l:
phase = ({"a": 0, "b": 1, "rc": 2}[pre_l], int(pre_n))
elif dev and not post:
phase = (-1, 0) # 2.0.dev1 sorts before 2.0a1
else:
phase = (3, 0) # final
post_k = int(post) if post else -1
dev_k = int(dev) if dev else float("inf") # no dev marker sorts last
return (tuple(nums), phase, post_k, dev_k)
versions = ["2.0", "2.0.post1", "2.0rc1", "2.0.dev1", "2.0a1", "2.0.1", "2.0b1", "2.10", "2.9", "2.0a1.dev1"]
print(" < ".join(sorted(versions, key=key)))
print(key("2.0") == key("2.0.0"), key("2.10") > key("2.9"), "2.10" > "2.9")2.0.dev1 < 2.0a1.dev1 < 2.0a1 < 2.0b1 < 2.0rc1 < 2.0 < 2.0.post1 < 2.0.1 < 2.9 < 2.10
True True False
The last value is the string comparison, "2.10" > "2.9", which is wrong: that one bug is why every tool ships a real version parser.
Test yourself
What does ~=2.31 allow?
- Only 2.31.x
- Any 2.x from 2.31 on (>=2.31, ==2.*)
- 2.31 and 3.x
- Exactly 2.31
Which list is in correct PEP 440 order?
- 2.0 < 2.0rc1 < 2.0.post1
- 2.0.dev1 < 2.0a1 < 2.0rc1 < 2.0 < 2.0.post1
- 2.0a1 < 2.0.dev1 < 2.0
- 2.0.post1 < 2.0 < 2.0.1
Is ^2.31 a valid PEP 440 specifier?
- Yes, it means >=2.31,<3
- No, caret is Poetry syntax; Poetry converts it to >=2.31,<3.0.0 in standard metadata
- Yes, it means ==2.31.*
- Only in requirements.txt