How to Install a Specific Version of a Package in Python?

Recently, one of my developers was struggling to install a specific version of a package in Python. I explained different methods with examples. In this tutorial, I will show you how to install a specific version of a package in Python using pip, Python’s primary package installer.

To install a specific version of a package in Python using pip, you can use the == operator followed by the desired version number. For example, to install version 1.19.5 of the numpy package, you would run the command pip install numpy==1.19.5. This ensures that exactly the specified version is installed, which is crucial for compatibility and stability in your projects.

Install a Specific Version of a Package in Python

There are several reasons you might want to install a specific version of a package:

  • Compatibility: Ensuring that the package works well with other dependencies.
  • Stability: Using a version that you know is stable and well-tested.
  • Development: Testing how your code behaves with different versions of a package.

Method 1: Using the == Operator

The simplest way to install a specific version of a package in Python is by using the == operator followed by the version number.

Here is an example.

Let’s say you want to install version 1.19.5 of the numpy package. You can do this by running:

pip install numpy==1.19.5

This command tells pip to install exactly version 1.19.5 of numpy.

Method 2: Specifying Version Ranges

Sometimes, you might want to specify a range of acceptable versions. You can do this using comparison operators.

Here is an example.

To install any version of requests that is greater than or equal to 2.20.0 but less than 3.0.0, you can use:

pip install "requests>=2.20.0,<3.0.0"

Method 3: Using a requirements.txt File

If you’re managing a project with multiple dependencies, it’s common to list them in a requirements.txt file. This file can also specify versions.

Here is an example that we have implemented while working on a Python project.

Create a requirements.txt file with the following content:

numpy==1.19.5
requests>=2.20.0,<3.0.0

Then, install all the packages listed in the file by running:

pip install -r requirements.txt

Method 4: Check Available Versions

Before installing a specific version, you might want to check which versions are available. You can do this using the pip index versions command.

To see all available versions of the pandas package, run:

pip index versions pandas

Method 5: Using the –upgrade Flag

If you want to upgrade a package to a specific version in Python, you can use the --upgrade flag.

Here is an example.

To upgrade scipy to version 1.6.2, use:

pip install --upgrade scipy==1.6.2

Check out How to Install Python on Windows or MAC?

How to Install a Specific Version of a Package in npm

I will show you various methods to install a specific version of a package using npm (Node Package Manager) in Python.

Method 1: Using the @ Symbol

The best way to install a specific version of a package is by using the @ symbol followed by the version number.

Here is an example.

To install version 4.17.1 of the express package, you can run:

npm install express@4.17.1

This command tells npm to install exactly version 4.17.1 of express.

Method 2: Specifying Version Ranges

Sometimes, you may want to specify a range of acceptable versions. This can be done using semantic versioning operators.

Let me show you an example.

To install any version of lodash that is greater than or equal to 4.0.0 but less than 5.0.0, you can use:

npm install lodash@^4.0.0

In this command, ^4.0.0 ensures that npm installs the latest minor and patch updates for version 4.

Method 3: Using a package.json File

If you are managing a project with multiple dependencies, it’s common to list them in a package.json file. This file can also specify versions.

Let me show you an example.

Add the following entry to your package.json file:

{
  "dependencies": {
    "express": "4.17.1",
    "lodash": "^4.0.0"
  }
}

Then, install all the packages listed in the file by running:

npm install

Method 4: Using the –save-exact Flag

If you want to ensure that npm saves the exact version of a package in your package.json file, you can use the --save-exact flag.

Here is an example.

To install version 2.29.1 of axios and save it exactly in package.json, use:

npm install axios@2.29.1 --save-exact

Conclusion

I hope you now know how to install a specific version of a package in Python using different methods. I have also shown how to install a specific version of a package in NPM in Python.

Do let me know if you still have any questions.

You may also like:

Leave a Comment