Explain Codes LogoExplain Codes Logo

How do I check the versions of Python modules?

python
prompt-engineering
importlib-metadata
virtual-environments
Alex KataevbyAlex Kataev·Sep 29, 2024
TLDR

To retrieve the version of a Python module, you can use the __version__ attribute directly in your Python code or run the pip show command in your command-line interface.

# You, to numpy: "Hey numpy, what edition are you?" import numpy print(numpy.__version__)
# In the terminal, when you feel too tired to import numpy pip show numpy | grep Version

These two methods swiftly get you the installed version of the Python module you're looking into.

Miscellaneous ways to check version details

There are plenty of ways to probe the version details of Python packages and modules. Here, we'll explore them at the command line level to Python's built-in facilities.

Communicating with Python from the terminal

Whether it's Linux, MacOS, or Windows, your command-line interface has got you covered:

  • On Linux/Mac:
# To check for a specific package version # Salvation: pip freeze | grep <package_name> # Or, to upgrade pip itself because hey, pip needs love too! pip install --upgrade pip
  • On Windows:
# For specific package version checking: # Sherlock Holmes mode pip freeze | findstr <package_name> # To upgrade pip because updates are good (usually!) python -m pip install --upgrade pip

Digging deeper into Python

Sometimes, you've got to go deeper:

# Digging deeper in Python when `__version__` attribute is off the grid import pkg_resources pkg_resources.get_distribution('package_name').version

And starting Python 3.8, you can even go meta:

from importlib.metadata import version version('package_name')

Harnessing the power of virtual environments

For project isolation, use tools such as virtualenvwrapper:

# Run this magic command inside a virtual environment pip freeze # BAM! Get all the packages and their versions

From easy_install to pip: an evolution story

Transitioning from easy_install to pip is like upgrading from a bicycle to a sports car. With pip, you unlock commands like:

# Show package details including the version. Mmm, details... pip show <package_name>

Remember to be precise with package names, as the exact name you input should correspond to the PyPI listing.

What if there is no __version__?

Some modules got attitude, they don't have a __version__:

try: import module print(module.__version__) except AttributeError: print("This module thinks it's too cool for a __version__.")

Plugging into importlib metadata

As Python development evolves, the importlib metadata library is becoming the new norm for retrieving package versions:

from importlib import metadata metadata.version('package_name') # Ain't nobody got time for __version__

Using virtual environments for peace of mind

Virtual environments back up your project environment. This way, you can replicate environments with ease using requirements.txt files generated by pip freeze.