Python Check Version of Package with pip

Python Check Version of Package with pip

Problem Formulation

Assuming you have the Python package manager pip installed in your operating system (Windows, Linux, macOS).

How to check the version of a package with pip?

Method 1: pip show

To check which version of a given package is installed, use the pip show <your_package> command. For example, to check the version of your NumPy installation or virtual environment, run pip show numpy in your command line or Powershell (Windows), or terminal (macOS and Linux/Ubuntu).

This will work if your pip installation is version 1.3 or higher—which is likely to hold in your case because pip 1.3 was released a decade ago in 2013!!

Here’s an example in my Windows Powershell, I’ve highlighted the line that shows that my package version is 1.21.0:

PS C:\Users\xcent> pip show numpy
Name: numpy
Version: 1.21.0
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email: None
License: BSD
Location: c:\users\xcent\appdata\local\programs\python\python39\lib\site-packages
Requires:
Required-by: pandas, matplotlib

In some instances, this will not work—depending on your environment. In this case, try those commands before giving up:

PS C:\Users\xcent> python -m pip show numpy
PS C:\Users\xcent> python3 -m pip show numpy
PS C:\Users\xcent> py -m pip show numpy

Of course, replace “numpy” with your particular package name.

Method 2: pip list

To check the versions of all installed packages, use the pip list command. You can then locate the version of your particular package in the resulting output. The fact that the output packages are sorted alphabetically may find to locate the particular package.

This will work if your pip installation is version 1.3 or higher.

Here’s an example in my Windows Powershell, I’ve highlighted the line that shows that my package version is 1.21.0:

PS C:\Users\xcent> pip list
Package         Version
--------------- ---------
beautifulsoup4  4.9.3
bs4             0.0.1
certifi         2021.5.30
chardet         4.0.0
cycler          0.10.0
idna            2.10
kiwisolver      1.3.1
matplotlib      3.4.2
mss             6.1.0
numpy           1.21.0
pandas          1.3.1
Pillow          8.3.0
pip             21.1.1
pyparsing       2.4.7
python-dateutil 2.8.1
pytz            2021.1
requests        2.25.1
setuptools      56.0.0
six             1.16.0
soupsieve       2.2.1
urllib3         1.26.6

In some instances, this will not work—depending on your environment. Then try those commands before giving up:

PS C:\Users\xcent> python -m pip list
PS C:\Users\xcent> python3 -m pip list
PS C:\Users\xcent> py -m pip list

Method 3: Check Package Version in Your Python Script

An alternative is to check your package installation in your Python script by first importing the library with import your_package and then call print(your_package.__version__). This will print the package version for most packages. However, it is not required for Python packages to provide the __version__ attribute, so it’s not 100% reliable.

Here’s the code:

import numpy
print(numpy.__version__)
# 1.21.0

Method 4: importlib.metadata.version

A more general way to check the package version in your Python script is provided by the importlib.metadata package in the form of a version(your_package_name_string) function. Calling it will return a string representation of the specific version. For example, importlib.metadata.version('numpy') returns the version 1.21.0 in my current Windows environment.

Here’s the code:

import importlib.metadata
print(importlib.metadata.version('numpy'))
# 1.21.0

That’s it!

Summary

In this article, we’ve studied four ways to check the package version using pip:

  • Method 1: pip show your_package
  • Method 2: pip list
  • Method 3: your_package.__version__
  • Method 4: importlib.metadata.version

Thanks for giving us your valued attention — we’re grateful to have you here! 🙂

Where to Go From Here?

Enough theory. Let’s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. That’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

🚀 If your answer is YES!, consider becoming a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!

Programming Humor – Python

“I wrote 20 short programs in Python yesterday. It was wonderful. Perl, I’m leaving you.”xkcd