Python offers various packages to perform different operations/tasks, such as image manipulation, computer vision, data analysis, etc. The pip is a renowned package manager that is used to install, uninstall, upgrade, and check versions of packages.
This post provides various methods to check the installed version of any package with pip in Python. The below-listed methods will be discussed in this guide:
- Method 1: Using pip show
- Method 2: Using pip list
- Method 3: Using pip freeze
- Method 4: Using “__version__” Attribute
- Method 5: Using importlib.metadata Package
Method 1: Using pip show
The easiest way to check the version of any package with pip is by using the following command in the CMD terminal:
> pip show <package_name>
The above template retrieves the package version along with other details related to the package.
Code:
> pip show pandas
The “pip show” command is used to check the installed version of the Pandas package.
Output:
The output snippet demonstrates that Pandas’ “1.5.2” version is installed on our system.
Method 2: Using pip list
The pip list command is used to show all the installed packages with their version in Python. Here is an example code:
Code:
> pip list
Output:
The above snippet shows all the installed packages along with their versions.
Method 3: Using pip freeze
The “pip freeze” command is also used to display all installed packages with their versions in freeze format. Here is an example code:
Code:
> pip freeze
Output:
The stated command shows the list of all installed packages along with their versions in freeze format.
Method 4: Using “__version__” Attribute
The “__version__” attribute is used to check the version of the package with pip in Python:
Code:
import pandas
print(pandas.__version__)
- The module named “pandas” is imported.
- The “pandas.__version__” attribute is used to check the installed version of the package with pip.
Output:
The above snippet shows that currently, Pandas’ “1.5.2” version is installed on our machine.
Method 5: Using importlib.metadata Package
The “version()” function of “importlib.metadata” package is used in the below code to check the version of a specific package:
Code:
import importlib.metadata
print(importlib.metadata.version('pandas'))
- The “importlib.metadata” is imported.
- The “importlib.metadata.version()” function accepts the package name as an argument and returns the version of that module in string.
Output:
Pandas version “1.5.2” is currently installed in our Python.
Conclusion
To check the version of the package with pip, the “pip show <package_name>”, “pip list”, and “pip freeze” commands are used in Python. The “__version__” attribute and the “importlib.metadata.version()” function can also be used in Python to get the version of any installed package. This guide demonstrated various methods to get/check versions of a specific package with pip.