note.nkmk.me

Check the version of Python package / library

Posted: 2019-09-20 / Tags: Python

This post introduces how to check the version of packages (libraries) and modules used in Python scripts, and the version of packages installed in the environment.

  • Get the version in Python script: __version__ attribute
  • Check with pip command
    • List installed packages: pip list
    • List installed packages: pip freeze
    • Check details of installed packages: pip show
  • Check with conda command: conda list

If you want to check the version of Python itself, see the following post.

Sponsored Link

Get the version in Python script: __version__ attribute

To get the version of a package used in a Python script, use __version__ attribute.

import pandas as pd

print(pd.__version__)
# 0.22.0

__version__ attribute is recommended by PEP (Python Enhancement Proposals) and many packages have it.

Note that __version__ attribute is not mandatory, so some packages do not have it.

In addition to the __version__ attribute, there are packages that provide functions and attributes that display more detailed information.

Note that __version__ is not set for the standard library modules such as math and os. Modules in the standard library do not have individual versions, but follow the Python version.

Check with pip command: pip list, pip freeze, pip show

If you are using the Python package management system pip, you can check the information of the installed package with the following command. Execute commands at the command prompt or terminal.

In some environments, use pip3 instead of pip. In some cases, pip is for Python2 and pip3 is for Python3.

List installed packages: pip list

pip list displays a list of installed package names and version numbers.

$ pip list
Package            Version
------------------ ---------
absl-py            0.1.10
agate              1.6.0
agate-dbf          0.2.0
agate-excel        0.2.1
agate-sql          0.5.2
appnope            0.1.0

There are four display formats: legacy,columns, freeze and json. It is specified with the pip configuration file pip.conf or --format <format-name> option.

The following options can be used with pip list.

  • -o, --outdated
    • List only out-of-date packages
  • -u, --uptodate
    • List only the latest packages

List installed packages: pip freeze

pip freeze command displays a list of installed package names and version numbers in freeze format.

$ pip freeze
absl-py==0.1.10
agate==1.6.0
agate-dbf==0.2.0
agate-excel==0.2.1
agate-sql==0.5.2
appnope==0.1.0

The difference between pip freeze and pip list --format freeze is that pip freeze does not display the following package management tools by default. If you add the --all option, all will be displayed.

  • pip
  • setuptools
  • distribute
  • wheel

If you save the output in freeze format as a text file, you can install packages in a specified version in a batch.

For such purposes, there is no need to list package management tools such as pip, so pip freeze does not output them by default.

Check details of installed packages: pip show

Use pip show <package-name> to display detailed information about a specific package.

In addition to version information, detailed information such as dependency packages and homepages are displayed.

$ pip show pandas
Name: pandas
Version: 0.22.0
Summary: Powerful data structures for data analysis, time series,and statistics
Home-page: http://pandas.pydata.org
Author: The PyData Development Team
Author-email: pydata@googlegroups.com
License: BSD
Location: /usr/local/lib/python3.6/site-packages
Requires: pytz, numpy, python-dateutil
Sponsored Link

Check with conda command: conda list

If you have built a Python environment with Anaconda, conda list will list the packages installed in the current virtual environment.

If the environment is not activated, use conda list -n <environment name>.

Sponsored Link
Share

Related Categories

Related Posts