Coding

pip Install Specific Version of a Package (==, Ranges, Downgrade)

To install a specific version of a package with pip, add == and the version after the name, in quotes: python -m pip install 'SomePackage==1.0.4'. Use double quotes instead of single quotes on Windows.

The command

# macOS / Linux
python -m pip install 'requests==2.32.3'

# Windows (Command Prompt or PowerShell)
py -m pip install "requests==2.32.3"

The package name and version above are placeholders for your own. The pip documentation shows exactly this form ('SomePackage==1.0.4') and uses single quotes in its macOS and Linux examples and double quotes in its Windows examples. Run it inside a virtual environment so the version goes into your project and not the system Python. If pip refuses with externally-managed-environment, see how to fix that error.

How to see which versions exist

List every version the index offers for a package:

python -m pip index versions peppercorn

Example output, taken from the pip documentation:

peppercorn (0.6)
Available versions: 0.6, 0.5, 0.4, 0.3, 0.2, 0.1

The number in brackets is the newest version. Pick one from the list and pin it. On Windows the pip documentation writes the command with py -m pip.

Why quote the specifier

The shell reads > and < as redirection, so an unquoted pip install requests>=2.30 is cut at the > and the rest is treated as an output file name. Quotes hand pip the whole string. Quote every specifier that contains a comparison operator, and it costs nothing to quote == too.

Version specifiers: exact, minimum, range

Besides ==, pip accepts the standard operators defined in the Python Packaging User Guide:

SpecifierMeaning
'pkg==1.4.2'Exactly 1.4.2
'pkg>=1.4.2'1.4.2 or newer
'pkg>=1.2,<2.0'Any version from 1.2 up to, but not including, 2.0
'pkg!=1.4.2'Anything except 1.4.2
'pkg==1.4.*'Any 1.4.x release (prefix match)
'pkg~=1.4.5'Compatible release: the same as >=1.4.5, ==1.4.*

The specification gives the compatible release example ~= 2.2, which is equivalent to >= 2.2, == 2.*, and says the operator must not be used with a single-segment version such as ~=1. Combine clauses with a comma, and pip picks the newest version that satisfies all of them.

Install an older version (downgrade)

Name the older version the same way. If a newer version is installed, pip replaces it:

python -m pip install 'requests==2.31.0'

If pip says the requirement is already satisfied, the installed version already matches your specifier. The pip documentation notes that pip install prefers to leave the installed version as it is unless --upgrade is given, which is why pip install requests alone does not update anything. To reinstall the same version even though it is up to date, add --force-reinstall, which the documentation describes as reinstalling all packages even if they are already up to date.

Install the latest version again

Go back to the newest release with the upgrade flag:

python -m pip install --upgrade requests

The documentation says --upgrade upgrades all specified packages to the newest available version, and that how dependencies are handled depends on the upgrade strategy.

Install a pre-release version

Pre-releases (alpha, beta, release candidate, development builds) are skipped by default. The packaging specification says they are implicitly excluded from all specifiers unless already installed or explicitly requested. Two ways to ask for one:

  • Pin it exactly: python -m pip install 'SomePackage==2.0a1'
  • Allow pre-releases: python -m pip install --pre SomePackage. The pip documentation says this includes pre-release and development versions, while by default pip finds only stable versions.

Pin a version without installing its dependencies

The version you name applies to that package only. Its dependencies are picked by pip's resolver. To install just the one package, add --no-deps, which the documentation describes as "Don't install package dependencies." Use it carefully: the package may fail at import time if a dependency is missing.

Pin versions in requirements.txt

The same specifiers work one per line in a requirements file:

requests==2.32.3
click>=8.1,<9

Quotes are not needed there because no shell is involved. Install with python -m pip install -r requirements.txt. The requirements.txt guide covers the file format, pip freeze and the "No matching distribution found" error.

If the install fails

  • pip cannot find the version. Compare your spelling with the output of python -m pip index versions <package>. A version may also be missing for your Python version or platform.
  • The shell reports a syntax error or creates a file. You left out the quotes around a specifier containing > or <.
  • Nothing changes. The installed version already satisfies the specifier. Pin it exactly with ==, or add --force-reinstall.
  • The package imports from the wrong place. Use python -m pip so that pip and the interpreter you run are the same installation.

Frequently asked questions

How do I install a specific version of a package with pip?
Put == and the version after the package name and quote the whole specifier: python -m pip install 'SomePackage==1.0.4' on macOS and Linux, or python -m pip install "SomePackage==1.0.4" in Windows Command Prompt and PowerShell. This is the form shown in the pip documentation.
Why do I need quotes around the version?
Characters such as > and < are shell redirection operators, so an unquoted 'package>=1.0' is split by the shell before pip sees it. Quotes pass the whole specifier to pip intact. A plain == specifier usually works unquoted, but quoting is the safe habit and matches the pip documentation.
How do I install a pre-release version?
Pin it exactly, for example 'SomePackage==2.0a1', or add --pre. The pip documentation says pip only finds stable versions by default, and --pre includes pre-release and development versions.
Does pip install a specific version of the dependencies too?
No. The specifier applies only to the package you named. Its dependencies are chosen by pip's resolver within the ranges that package declares. To pin those as well, list them in a requirements file or a constraints file.

Sources