Install a specific version of a Python package using PIP

David Y.
jump to solution

The Problem

How do I install a specific, older version of a Python package using PIP? Will it matter if I have another version already installed?

The Solution

We can install an older version of a specific package by using the following syntax in our PIP command:

pip install requests==2.30.0

The == syntax allows us to specify a version number. We can see which version numbers are available by looking at the release history on the package’s page on PyPI.org.

Note that, on Windows, the package name and version should be surrounded by quotation marks:

pip install "requests==2.30.0"

If a different version of the package is already installed, pip will automatically replace it with the specified version. The --force-reinstall flag is only needed when the requested version is the same as what is already installed, since pip would otherwise consider the requirement already satisfied.

To ensure that future users of our project install the correct package versions when setting it up, we should create a requirements file. This is a file in our project’s root directory named requirements.txt, which contains a list of requirement specifiers (i.e. package names and versions) on which our project depends. For this example, our requirements file would have one line:

requests==2.30.0

We would then be able to install the correct version of this and any other packages added by running the following PIP command:

pip install -r requirements.txt

To avoid changing the packages on our entire system for individual Python projects, we can use a virtual environment. This is the recommended way to deal with Python dependencies in most circumstances.

Location of Python `pip` packages
David Y.
Convert bytes to a string in Python
David Y.
Lowercase a string in Python
David Y.

Considered "not bad" by 4 million developers and more than 150,000 organizations worldwide, Sentry provides code-level observability to many of the world's best-known companies like Disney, Peloton, Cloudflare, Eventbrite, Slack, Supercell, and Rockstar Games. Each month we process billions of exceptions from the most popular products on the internet.

Sentry