Pip Install Specific Version of a Python Package: 2 Steps

In this Python tutorial, you will learn how to use pip to install a specific version of a package. The outline of the publication (as also seen in the ToC) is as follows. First, you’ll get a brief introduction with examples of when you might need to install, for example, an older version of a package. Secondly, you will get the general syntax of how to carry out this task. After that, you will get two steps to install specific versions of Python packages with pip. This section will also teach you how to work in a virtual environment. In the next section, we’ll look at how to specify the version of multiple Python packages by creating a .txt file.

Why

install an older version of a package?

Now, there may be several reasons why you might want to install a specific version of a Python package. For example, you might need to install an older version of a package if the package has changed in a way that is incompatible with the version of Python you installed, with other packages you installed, or with your Python code. As mentioned above, we will work with the package manager, pip. Still, it is also possible to install a specific version of a package if you use other package managers. For example, it is also possible if you use the conda package manager (Anaconda Python distribution).

Here are some instructions on how to use pip to install a specific (e.g. older) version of a Python package: pip syntax for installing a specific version of a package

Here is the general Pip syntax that you can use to install a specific version of a Python package

: pip install <PACKAGE>==<VERSION>Code language

:

Bash (bash)

As you will understand, now, you exchange “<PACKAGE>” and “<VERSION>” for the package name and the version you want to install, respectively. Don’t worry, the next section will show you, for example, more exactly how this is done.

If you receive the warning, as in the image above, you can upgrade pip to the latest version: pip -install upgrade pip. Before we get into more detail, here’s how

to install a specific version of a Python package:

In the next section, you will learn two important steps to install a certain version of the Python package using the pip package manager. First, you will learn how to install and create a virtual environment. Second, you’ll learn how to use pip to install a version you need of a Python package using the syntax you’ve already learned.

Two steps

to install a specific version of a package with pip:

In this section, you will learn how to install an older version of the Python package using pip. First, I would recommend creating a virtual environment. Therefore, you will first learn how to install the virtual environment package, create a virtual environment, and install a specific version of a Python package.

1) Install virtualenv and create an environment

First, you must install the virtualenv package. Here’s how to

install a Python package with pip: pip install virtualenvCode language: Bash (bash)

In the code snippet above, we installed a Python package called pip “virtualenv”. Virtualenv

is a tool used to create isolated Python environments. We use it to create a separate environment for each project, with its own set of dependencies and Python version. This helps avoid conflicts between different projects that may require different versions of the same package.

When we run this command, pip will download the virtualenv package from the Python Package Index (PyPI) and install it on your system. After the installation is complete, you can use the virtualenv command to create new virtual environments for your Python projects.

Here’s how we create and then activate a

virtual environment: virtualenv myproject source myproject/bin/activateCode language: Bash (bash)

Now that your virtual environment is set up, you can proceed to the next step and install an older version of the Python package. In step two, we used pip again (like when we installed virtualenv) but now we’ll also use the general syntax we’ve learned earlier in this post.

2) Install the specific version you need with Pip

Now that your virtual environment is ready to use. Here’s how to use

pip to install a specific version of the Pandas package: # Use pip to install a specific version: pip install pandas==1.1.1Code language: Bash (bash) In the code snippet

above, we can see an example when we run pip in the command line interface (CLI) to install a specific version of the Pandas library for Python. Here’s a

breakdown of the code snippet:

pip is a

  • package installer for Python. install
  • is the command being

  • executed
  • . pandas is the name of the library being installed.

  • ==1.1.1 is a version specifier that tells pip to install version 1.1.1 of the Pandas library.

So when you run pip install pandas == 1.1.1

in your command line interface, pip will download and install version 1.1.1 of the panda library. Voilá! We have successfully installed a specific version of a package using pip.

Of course, it is possible to add more packages and their versions if you have many packages that you want to install a certain version. However, this can be cumbersome, and in the next section, we’ll look at how to install older versions of multiple packages. That is, when they are stored in a text file.

Dealing with multiple packages and installing specific versions

That was pretty simple, but using the steps above may not be helpful if, for example, you need to install a lot of Python packages. When we install packages using pip we can create a .txt file (e.g. requirements.txt). Here is a sample text file with some Python packages and their versions:

As you can see, you need to keep each package on a line in the text file. Also, you should follow the syntax you learned earlier in the post. This is also evident in the image above. Here’s how

to install a specified version of multiple packages using the text file: # Pip install specific versions of multiple packages: pip install -r myproject/requirements.txtCode language: Bash (bash) The preceding code snippet is a command

in a command line interface (CLI) to install Python dependencies for a

project.

Here’s a breakdown of the code snippet:

  • As we already know, pip is a package installer for Python
  • .

  • install is the command that is running.
  • -r stands for “requirements”, and will cause pip to read a file’s list of dependencies instead of specifying them directly on the command line.
  • myproject/requirements.txt is the file that contains the list of dependencies.

So when you run pip install -r myproject/requirements.txt in its command line interface (i.e. bash), pip will read the requirements.txt file in the myproject directory and install all the dependencies listed in that file.

Installing an earlier version of a package can cause problems with package dependencies. You will still get the latest versions of dependencies. That is, the version you use allows, of course. A downside to this is that you can then break your app or workflow. Fortunately, there are some solutions to combat this problem. For example, if you want your data analysis to be reproducible, using Binder, Jupyter Notebooks and Python can be a solution. However, if you are developing applications, you may need another strategy. In the last section, we’ll take a look at another Python package that may be useful: Pipenv (see the resources at the bottom for a great tutorial on Pipenv).

Conclusion

In this short Python tutorial, you learned how to use pip to install a certain version of the package. First, he learned pip syntax to specify a version. After that, he learned how to 1) create a virtual environment and 2) install the version of a package he needed. In the final section, we looked at how to deal with multiple packages of certain versions. How to set the version of multiple packages that you want to install.

If you have any suggestions or corrections to the current post, please comment below. I always appreciate when I learn from others.

Resources

Here are some useful packages and tutorials, as well as documentation that may be worth checking

out:

  • Pipx: Installing, Uninstalling, and Updating Python Packages in Virtual Envs
  • Pipenv

  • Virtualenv
  • Documentation

  • Pip
  • Documentation Pipenv
  • Guide