Sentry Answers>Python>

Install a specific version of a Python package using PIP

Install a specific version of a Python package using PIP

David Y.

The ProblemJump To Solution

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:

Click to Copy
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:

Click to Copy
pip install "requests==2.30.0"

If a newer version of the package is already installed on our system or in our current virtual environment, we will need to add the --force-reinstall flag to uninstall that version first:

Click to Copy
pip install --force-reinstall requests==2.30.0

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:

Click to Copy
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:

Click to Copy
pip install -r requirements.txt

Again, we may need to provide the --force-reinstall flag when running this command.

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.

  • Sentry BlogPython Performance Testing: A Comprehensive Guide
  • Sentry BlogLogging in Python: A Developer’s Guide
  • Syntax.fm logo
    Listen to the Syntax Podcast

    Tasty Treats for Web Developers brought to you by Sentry. Web development tips and tricks hosted by Wes Bos and Scott Tolinski

    Listen to Syntax

Loved by over 4 million developers and more than 90,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.

© 2024 • Sentry is a registered Trademark
of Functional Software, Inc.