Python developers need to have total control over their virtual environments, including versions of the installed packages. This isolates packages and avoids dependency conflicts when you are working on several projects.
These methods will help you with dependency management and allow you to check package versions in Python.
How To Check Package Versions In Python
Many packages have a __version__ attribute, which shows the version of that package. This is an optional attribute that many developers provide voluntarily as a convenient way to check for package versions in Python.
>>> import numpy >>> print(numpy.__version__) '1.22.4' >>> import pandas >>> print(pandas.__version__) '1.4.2'
These attributes are strings that, per PEP8’s official recommendations, are placed before any import statements and right after the module docstring (except for the “from __future__” imports).
It is important to note that these attributes aren’t mandatory. Some Python packages may break the convention and don’t include them.
Additionally, there is __version__ for standard library modules. They follow the version of your Python installation and don’t have individual versioning. Accessing this attribute of those packages only returns errors from the AttributeError exception.
>>> import youtube_dl >>> youtube_dl.__version__ Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'youtube_dl' has no attribute '__version__' >>> import math >>> print(math.__version__) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: module 'math' has no attribute '__version__'
Developed by Python Packaging Authority (PyPA), Package Installer for Python (pip) is the most widely used package management system for Package packages. Pip comes with the default Python installation in most systems.
There are many sub-commands of pip, some of which can be used to find out the version of an installed Python package.
The most straightforward option is pip show, whose job is to display information about a package.
$ pip show pandas Name: pandas Version: 1.4.2 Summary: Powerful data structures for data analysis, time series, and statistics Home-page: https://pandas.pydata.org Author: The Pandas Development Team Author-email: [email protected] License: BSD-3-Clause Location: /usr/lib/python3.10/site-packages Requires: python-dateutil, pytz, numpy Required-by:
You can do this with packages without a __version__ attribute.
$ pip show youtube_dl Name: youtube-dl Version: 2021.12.17 Summary: YouTube video downloader Home-page: https://github.com/ytdl-org/youtube-dl Author: Ricardo Garcia Author-email: [email protected] License: Unlicense Location: /usr/lib/python3.10/site-packages Requires: Required-by:
Note: if you have trouble upgrading pip in Windows, follow this guide.
If you want to check package versions through a Python statement with the widest support, have a look at the importlib module and its metadata library.
Designed as the import system of Python, it can provide metadata of every installed package. This is the recommended method to find detailed information about a specific package in Python.
>>> from importlib.metadata import version >>> version('pandas') '1.4.2' >>> version('numpy') '1.22.4' >>> version('youtube_dl') '2021.12.17'
You can check package versions in Python in several ways. Some of them can be done right inside the interpreter, while the other (pip) works on your command line. Make sure you understand where you can use each option.
Leave a comment