What is Pip?
Pip is Python’s package manager. It is used all over the world to install and manage libraries and dependencies for projects with varied requirements. Like we learnt in our NVM guide, different projects require specific versions of libraries. This is to ensure compatibility, maintain consistency, and prevent conflicts in both production and development environments. In pip, installing a specific version is easy and it has just got easier with Unixmen guiding you through the process. Pip allows you to control the exact Python package version used in your projects. Today, let us walk you through Pip, installing a specific version of a python version, with a few examples.
Why Would You Require a Specific Version?
With Pip, installing a specific version of a package becomes essential in situations like:
- Maintaining a consistent environment: To ensure your code behaves as expected in production and development environments, it is preferred to use the same package version.
- Compatibility with multiple versions: There might be a few projects that depend on specific versions of libraries which contain requirement specific features and functionalities.
- Conflicts due to dependencies: Some older projects might face flow breaks with the latest versions of libraries. Using a stable version helps avoid dependency conflicts.
Basic Syntax to Install Specific Version with Pip
Here is the basic syntax in Pip to install a specific version of a package:
pip install PackageName==VersionNumber
For instance, let us try to install version 1.18.0 of the numpy package. Here is the command to do so:
pip install numpy==1.18.0
In all other commands, there is usually one equal sign. The double equal sign mentioned here instructs Pip to install the exact version you have specified. Executing this command ignores even newer available versions and installs the version we have specified in the command.
Detailed Guide to Install a Specific Version
Here is a step-by-step guide to install a specific package version with Pip.
How to Check the Available Versions
Before you finalize a version, it is important to first know the versions that are available to download. To view the list of all versions of a package, execute the command:
pip install PackageName==
When this command is executed, pip lists all the available versions. To give you a sample, let us try to write the command to list all available versions of numpy:
pip install numpy==
The output will list all the versions of numpy that can be installed. This helps you to choose the version that suits your project the most.
Steps to Install the Desired Version
Now that we have listed all the available versions, the next step is to identify the version we want to install. We can use the pip command to install specific versions.
pip install PackageName==VersionNumber
For example, if you would like to install version 1.18.0 of numpy, execute the command:
pip install requests==2.25.0
Check the Version of Installed Package
After installing, you can use pip to verify the version installed. The syntax for checking the version is:
pip show PackageName
Let us take the numpy package again to understand this command better. To know the version of numpy installed, run the command:
pip show requests
The output will display the details of the numpy package, which includes the installed version as well.
A Few Examples of Using Pip to Install Specific Versions
Now that we have learnt the basic syntax, let us see some practical examples where we use pip to install specific versions of packages commonly used in Python projects.
How to Install Specific Version of Django
Let’s say your development project requires you to install Django version 3.1.0. By using Pip, install specific version of Django by executing the command:
pip install django==3.1.0
Important point to note is that executing this command installs only version 3.1.0 even if a newer version is available.
How to Install a Specific Version of Flask
To install Flask version 1.1.1, execute the command:
pip install flask==1.1.2
As explained earlier, running this command installs the version specified in the command even if more recent versions exist.
How to Install Multiple Packages with Specific Versions
In cases where you would need to install multiple Python packages, you need not wait and execute one command for each package. With Pip, you can install specific versions of packages for multiple packages at once. Let us understand this setup with an example.
If you would like to install version 1.18.0 of numpy and version 3.1.0 of Django at once, execute the command:
pip install numpy==1.18.0 django==3.10
Replace the package names and versions with the packages and versions of your choice. The above command installs both numpy and Django packages at once.
Best Practices for Package Management with Pip
To have your setup managed better, organized, and avoid conflicts, let us provide you some best practices.
- Minimize using sudo with pip: Always install packages locally or in virtually contained environments. Reserve sudo for system wide installations.
- Check compatibility before installation: Always test any package update in a separate environment to ensure compatibility.
- Periodically review dependencies: Schedule reviews for package dependencies especially when you are updating packages.
- Prefer virtual environments: It is better to create virtual isolated environments for each project to prevent unwanted dependency issues.
Some FAQs You may Have
How can I downgrade a package using pip?
You can move to a lower version of a package by specifying the version number you would like to downgrade to.
Where can I view the current version of an installed package?
You can use the pip show command to view the details of the package installed, which also includes the version number.
Can I install pre-release versions of a package using pip?
Yes. Just add “–pre” at the end of the command to let pip know that you are installing pre-release versions.
Wrapping Up
You can be either working on a HomeLab setup or with a team in an enterprise setup where you would have to manage specific package versions. Pip is your trustable friend in these situations. One best practice is to use requirements.txt for avoiding conflicts, easier replication, and leveraging virtual environments. Use pip to install specific versions of packages and manage Python packages with ease.