The Fastest Way To Check Package Versions In Python

check package versions in python

Python has a lot of packages for website development, scripting, machine learning, data analysis, and more. There are projects with hundreds of packages. So this tutorial will introduce how to check package versions in Python. Continue reading the article to know.

Check package versions in Python quickly

A package in Python is a directory containing one or more modules. It was created to group modules with the same functionality for easier source code management.

Here we will show you the easiest way to check the package version.

Using pip show

This way is the easiest way to check the package version.

Pip is Python’s widely used package manager. By default, it is installed with Python. It is similar to the NPM. To check the version of the package you want, use the ‘pip show’ command and add the name of the package you want to check, like below:

pip show pandas

If you are using Python 3 on Linux or macOS, use the command below:

pip3 show pandas

Output:

Name: pandas
Version: 1.5.1

Using pip list

This way will list all installed packages using pip and the version of each package.

You need to type the command line below:

pip list

If you are using Python 3 on Linux or macOS, use the command below:

pip3 list

Output:

Package         Version
--------------- -------
altgraph        0.17.2
autopep8        1.7.0
future          0.18.2
macholib        1.15.2
numpy           1.23.4
pandas          1.5.1
pip             21.2.4
pycodestyle     2.9.1
python-dateutil 2.8.2
pytz            2022.5
setuptools      58.0.4
six             1.15.0
toml            0.10.2
wheel           0.37.0

Using pip list and findstr

This way only works on Windows.

To check the version of a package you want on Windows, you can use the command:

pip3 list | findstr <package you want to check version>

For instance, if we want to check the pandas package, we will use the following command line:

pip3 list | findstr pandas

Output:

pandas                    1.5.1

Using the __version__ attribute

First, enter the name of the library you want to check. Then you need to call the __version__ attribute on that library name, and you will see the version of that package. Like this:

import pandas

# Use the __version__ attribute to check the version of pandas
print(pandas.__version__)

Output:

1.5.1

Using importlib.metadata.version()

In this way, you must import the importlib.metadata library first. Then you can use version() to check the package version you want. Like this:

import importlib.metadata

# Use importlib.metadata.version() to check the version of pandas
print(importlib.metadata.version('pandas'))

Output:

1.5.1

Using pip freeze

Use the command line below:

pip freeze

In Python 3 on Linux or macOS, use the command below:

pip3 freeze

You will get a list of the packages and versions you have installed. Like this:

Output:

numpy==1.23.4
pandas==1.5.1
pycodestyle==2.9.1
python-dateutil==2.8.2

Summary

Through the 6 ways we have just shared with you above, we hope you have found the best way to check package versions in Python. If you have a more helpful way or any questions, please leave them in the comment section. Have a lucky day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *