- Summary: There are primarily two ways of installing a package with a specific version using
pip install -Iv package-name == version
pip install -v package-name == version
Problem Statement: How to install a specific package version with pip in Python?
pip is a command and tool used to install, update and remove third-party packages. Generally, it is used to install and update the packages, but there are many different use cases. By default, when we use the pip command, it will install the latest version of the specified package. However, Python is a dynamic programming language and has a dynamic environment, and hence, the package versions change regularly. Sometimes to run a Python code or script, a specific package may be required. In this article, we are going to learn how to install a specific Python package version with the
Why do we need to install a specific or an older version of pip in Python?
There are many reasons that you may want to install a specific old version of a Python package. If the available (new) package is not compatible with the version of Python you have installed or with other packages that you have installed or with your Python code, then you have to install the older version of a package. We can install the old version by using the package manager pip or by using other package managers. For example, we can always use the package manager Conda (Anaconda Python distribution).
Steps to Install Specific Version of a Package with Pip
There are two important steps to installing a specific version of a Python package using the pip package manager. The first step is to learn how to install and create a virtual environment and the second step is to learn how to use the pip command to install the specific version in Python.
Step 1- Installing The Virtual Environment
To install a specific version, we have to first create a virtual environment. To create the virtual environment, we require the virtualenv package that can be installed with pip using the following command:
After installing the virtualenv, we must specify a path. Let’s say we want to create a virtual environment in the local directory called ‘
demo‘, then we need to use the following command:
Further, we need to activate the virtual environment by running the following command:
Note: The name of the virtual environment is available in the brackets on the terminal line e.g. (demo).
This is how the virtual environment is successfully created in Python. Now, any Python command will work within this virtual environment. Before upgrading the pip version, first, we must obtain the current pip version by running the following command:
If the version is not the specific version that you want, you can install the version of your choice using pip. Now to install an older or a specific version of pip, follow the next step.
Step 2- Installing A Specific Package Version In Python
The pip command uses the install option and the package name by default to install the latest version of the package. When we have to install the previous version of the package, i.e. a specific package version, we can use the == sign followed by the package name and the version of the package to specify the version that we want to install.
⚠️ Sometimes you may face the following error:
If you get the above warning, you can just upgrade the pip to the latest version using the following command:
For example, if you want to install the Pandas’ Python package of version 1.2.1, the following command can be used:
If we face any problems like errors or collusion, or if the package is already installed, we can force the installation of the specific package. To force install the package, we must use the
--force-install option as shown below:
Now, if we have installed a specific version that we need, and when we try to use the installed package we may still get an error. Different reasons may have caused the error but the most common reason is that the different versions can collide. Hence, to remove the other versions, we can use the
-Iv option as shown below:
--ignore-installedand this facilitates pip to ignore the already installed packages by overwriting them.
-vstands for verbose.
For more information, you can refer to the output of the following command:
pip install – help
How To Deal With Multiple Packages and Installing Specific Versions?
If we want multiple packages to get installed having a specific version, we can even do that with the help of a text file. The specific versions of the different packages must be defined within the text file. Consider that the file is named “
requirements.txt” file and it contains the following package definitions:
📄 requirements.txt file:
Now, to install the specific versions of the Python packages from the “requirements.txt” file, we have to use the following command:
Sometimes installing an older version of the packages can lead to some problems with the packages’ dependencies. We get the newest versions of the dependencies that the new version allows.
That is all about how to install a specific package version with pip in Python.I hope you found it helpful. Please stay tuned and subscribe for more such interesting articles. Happy Learning!
Authors: Rashi Agarwal and Shubham Sayon
One of the most sought-after skills on Fiverr and Upwork is web scraping . Make no mistake: extracting data programmatically from web sites is a critical life-skill in today’s world that’s shaped by the web and remote work. This course teaches you the ins and outs of Python’s BeautifulSoup library for web scraping.