This article explains how to check the versions of packages (libraries) and modules used in Python scripts, and the versions of packages installed in your environment.
Contents
- Get package versions in a Python script: __version__
- Check package versions with pip
- List installed packages: pip list
- List installed packages: pip freeze
- Check details of installed packages: pip show
- Check package versions with conda: conda list
To check the version of Python itself, refer to the following article.
Get package versions in a Python script: __version__
To get the version of a package used in a Python script, use the __version__
attribute.
import pandas as pdprint(pd.__version__)# 2.0.1
source: pandas_version.py
The __version__
attribute is recommended by Python Enhancement Proposals (PEP) and is commonly implemented across many packages.
- PEP 396 – Module Version Numbers | peps.python.org
- PEP 8 – Style Guide for Python Code | peps.python.org
It is important to note that not all packages include the __version__
attribute, as it is not mandatory.
In addition to the __version__
attribute, certain packages, including NumPy and pandas, offer functions and additional attributes for accessing more detailed version information.
Note that the __version__
attribute is not set for the standard library modules such as math
and os
. These modules do not have individual versions but follow the Python version.
Check package versions with pip
For Python environments managed with pip
, you can check information about the installed packages using the following commands. Execute these commands in a command prompt or terminal.
In certain environments, you may need to use pip3
instead of pip
. For basic information on how to use pip
, such as installing, updating, and uninstalling packages, see the following article.
List installed packages: pip list
pip list
displays a list of installed package names and their version numbers.
$ pip listPackage Version------------------ ---------absl-py 0.1.10agate 1.6.0agate-dbf 0.2.0agate-excel 0.2.1agate-sql 0.5.2appnope 0.1.0...
pip list
supports the following options:
--format <format-name>
- Sets the display format (
columns
,freeze
,json
)
- Sets the display format (
-o
,--outdated
- Displays only out-of-date packages
-u
,--uptodate
- Displays only up-to-date packages
Refer to the following article for more information.
List installed packages: pip freeze
pip freeze
displays a list of installed package names and their version numbers in the freeze
format.
$ pip freezeabsl-py==0.1.10agate==1.6.0agate-dbf==0.2.0agate-excel==0.2.1agate-sql==0.5.2appnope==0.1.0...
Unlike pip list --format freeze
, pip freeze
does not include package management tools such as pip
, setuptools
, distribute
, and wheel
by default. These can be included by using the --all
option.
Saving the freeze
format output to a text file allows for bulk installation of packages at specified versions. Since including package management tools is generally unnecessary for this purpose, they are omitted by default in pip freeze
.
Check details of installed packages: pip show
pip show <package-name>
displays detailed information about a given package.
It displays detailed information including the version, license, dependent packages, etc.
$ pip show pandasName: pandasVersion: 2.0.1Summary: Powerful data structures for data analysis, time series, and statisticsHome-page: Author: Author-email: The Pandas Development Team <pandas-dev@python.org>License: BSD 3-Clause License...Location: /opt/homebrew/lib/python3.11/site-packagesRequires: numpy, numpy, python-dateutil, pytz, tzdataRequired-by:
Check package versions with conda
: conda list
For Python environments managed with Anaconda, use conda list
to view installed packages in the current virtual environment.
When the environment is not activated, use conda list -n <environment-name>
.