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 its version?

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. Then 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.

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.

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

Where to Go From Here?

Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And 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?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become 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.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!