You can install a specific version of a Python package using Pip by running the pip install <package_name>==<version>
command. For example, to install version 1.3.4 of Pandas, execute the pip install pandas==1.3.4
command from the terminal.
2 Ways to Pip Install a Specific Version of a Python Package
- Uninstall and Install Method: First, run
pip uninstall <package_name>
, then executepip install <package_name>==<version>
. - Overwrite Method: Install the specific Python package and add
--ignore
installed to overwrite the previous version.
That’s the short version, but sometimes you’ll want to take more control over package installation. That’s where the rest of the article comes in.
Why Would You Want to Install a Specific Version of a Python Package?
You might be wondering, why even bother with older versions of Python packages? Well, maybe you have a huge codebase that’s not compatible with the most recent package update. Maybe you wrote the code years ago and it still works in production, but updating the package might break it. Or maybe even the most recent package version isn’t compatible with your Python version.
In other words, whatever the case may be, there are valid reasons for installing older versions of a Python package.
Let’s now go over some practical examples of how to install a specific package version with Pip.
How to Check Which Version of a Python Package is Installed
There are two useful commands you must know before proceeding to install a specific Python package version. These are:
pip show <packagename>
: This shows the currently installed version of the package, its summary, author, license, dependencies, and so on.pip index versions <packagename>
: This lists all the available package versions you can install.
Let’s check them out for the pandas Python package. The following shell command prints the currently installed version:
pip show pandas
It seems like Pandas version 1.5.3 is installed, and it requires numpy
, python-dateutil
and pytz
packages to run.
But is version 1.5.3 the latest, and which other versions can we install? That’s what the next shell command answers:
pip index versions pandas
Version 2.0.0 is the most recent, but you can go far, far back, depending on the Python version you have installed on your system or inside a virtual environment.
You now know how to check for the currently installed package version and also how to list all the available versions. Next, let’s see how to pip install a specific version of a package.
How to Install Pip Specific Version of a Python Package
There are two main ways to install a specific version of a Python package. The first one requires two commands, one to uninstall the current version, and the other to install the version you want. The second approach packs all that functionality in one shell command, so that’s the one you should use in practice.
Method 1: Uninstall and Install
First, identify which version of Pandas you have installed, and which versions you can potentially install according to your current Python version.
In our case, we have version 1.5.3, but we want to go back to version 1.3.4. To do so, we first have to uninstall the current version:
pip uninstall pandas
And now, pip install specific version of Pandas by running the following command:
pip install pandas==1.3.4
It seems like everything went fine, but how can you make sure version 1.3.4 was installed? Spoiler alert: You already know the command:
pip show pandas
Pandas 1.3.4 is successfully installed on our system, so we can mark our job here as done.
Method 2: Overwrite the Installed Package
The superior approach to the one from the previous section is to install a specific version of a Python package with a single shell command. You can do so by appending the --ignore-installed
flag when installing a specific version of the package.
Here’s an example: It installs Pandas version 2.0.0 and overwrites version 1.3.4, which was installed moments ago:
pip install pandas==2.0.0 --ignore-installed
And just to verify, you can use the pip show
command:
pip show pandas
And that’s two ways to pip install a specific version of a Pandas package. Will it always be that easy? Probably not, so continue reading for a more production-ready use case.
How to Install Multiple Python Packages with Specific Versions
Installing Python packages one by one is fine in your local environment, but it doesn’t scale in production. That’s where you’ll typically have a requirements.txt file, which contains all Python dependencies for the project alongside their specific version.
Wait, what is a trequirements.xt
file?
The requirements.txt
is typically structured like this:
package_1==version_1
package_2==version_2
...
In short, there’s a list of Python dependencies with their respective versions. The question now is: Can you install all of them with a single command? And the answer is yes, you can.
Here’s what the file looks like on our end:
And this is the command you should run if the requirements.txt
file is in the same directory as your shell:
pip install -r requirements.txt
If your requirements.txt file is located elsewhere, simply provide an absolute or relative path after -r
. That’s it!
Now, let’s verify the correct package versions were installed:
And that’s how you can install multiple Python packages at once. Let’s make a short recap next.
Pip Install Specific Version: Overview
Project dependency management doesn’t have to be difficult. You can use Python’s pipreqs
module to keep track of the packages used within a project and to create a requirements.txt
file, and then apply everything learned in this article to transfer the same set of dependencies to another machine or a production environment.
You can also pip install a specific version of a Python package one at a time, which is fine for a local development environment. A far better approach is to have a dedicated file for the dependencies, and install them all at once.
Frequently Asked Questions
How do you install a specific package version with pip?
There are two common ways to install a specific package version with pip:
- Uninstall and Install Method: First, run
pip uninstall <package_name>
, then executepip install <package_name>==<version>
. - Overwrite Method: Install the specific Python package and add
--ignore
installed to overwrite the previous version.
How do you install multiple specific package versions with pip?
You can install multiple specific package versions with pip using the requirements.txt file, which contains the specific package versions for a given project. If the requirements.txt
file is in the same directory as your shell, enter: pip install -r requirements.txt
. If it isn’t, provide an absolute or relative path after the -r
.