Sub topic 6
Pinning and constraining versions
This chapter assumes you can create and activate a virtual environment and run python -m pip install inside it. It covers how to say which versions of a package you accept.
Version specifiers
A version specifier is a rule attached to a package name. pip picks the newest release that satisfies every rule. With no specifier, pip installs the newest release, which may differ next month.
| Specifier | Meaning |
|---|---|
| `==2.32.3` | Exactly this version |
| `==2.32.*` | Any 2.32 release |
| `!=2.32.0` | Anything except this version |
| `>=2.31` | This version or newer |
| `<3` | Older than 3 |
| `~=2.31` | Compatible release: `>=2.31` and `==2.*` |
| `~=2.31.1` | `>=2.31.1` and `==2.31.*` |
Join several rules with commas. A release must satisfy all of them, so a comma works as "and".
python -m pip install "requests==2.32.3"
python -m pip install "requests>=2.31,<3"
python -m pip install "requests~=2.31"
python -m pip install "requests!=2.32.0,>=2.31"
python -m pip install "requests==2.32.*"Pins and ranges
Pinning with == fixes one exact version. This gives repeatable installs, but you receive no bug fixes or security patches until you change the pin.
A range such as >=2.31,<3 accepts newer releases, which means you get fixes. Your code may also meet a version you never tested. The upper bound protects you from a future major release that breaks the API. Most projects take this approach:
- A library lists wide ranges. It is installed alongside other packages, so tight pins make conflicts more likely.
- An application pins exact versions in a generated file, as the requirements chapter explains.
~= is a shorthand for "this release or later, but not the next major (or minor) series". Its meaning depends on how many parts you write. ~=2.31 allows 2.40, while ~=2.31.1 does not.
Checking what a specifier allows
You can preview pip's choice without changing the environment:
python -m pip install --dry-run "requests>=2.31,<3"
python -m pip show requests--dry-run resolves the requirement and reports what would be installed. pip show reports the version that is installed.
The same specifiers work in a requirements file, one per line:
requests>=2.31,<3
rich~=13.7Pre-releases and conflicts
pip ignores pre-releases such as 3.0.0rc1 unless you ask for one. It accepts them when a specifier names one (==3.0.0rc1) or when you pass --pre.
Specifiers that no release can satisfy produce an error like this:
ERROR: Could not find a version that satisfies the requirement requests<2,>=2.31
ERROR: No matching distribution found for requests<2,>=2.31Check for contradictory bounds first. Also check that the version exists for your Python version, because a release may require a newer interpreter than yours.
Conflicts can also come from two packages that need different ranges of a shared dependency. Then pip reports ResolutionImpossible and lists which requirements clash. Loosen one of your own bounds, or upgrade the package that has the stricter requirement.
Exercise
What does python -m pip install "pkg~=1.4.2" allow, and why would pkg-1.5.0 be rejected?
Show answer
It means >=1.4.2 and ==1.4.*, so it allows 1.4.2, 1.4.9 and any later 1.4 patch release. Version 1.5.0 is rejected because it falls outside the 1.4.* series.
Exercise
A teammate runs python -m pip install requests>=2.31 and finds a new file named =2.31 in the directory. What happened, and how do they fix it?
Show answer
The shell treated > as output redirection, so pip received only requests and its output went into the file =2.31. Delete the file and run the command again with the requirement in quotes: python -m pip install "requests>=2.31".