How to check version of python modules?


I just installed the python modules: construct and statlib with setuptools like this:

# Install setuptools to be able to download the following
sudo apt-get install python-setuptools

# Install statlib for lightweight statistical tools
sudo easy_install statlib

# Install construct for packing/unpacking binary data
sudo easy_install construct

I want to be able to (programmatically) check their versions. Is there an equivalent to python --version I can run from the command line?

My python version is 2.7.3.

7/28/2017 7:56:54 PM

Accepted Answer

I suggest using pip in place of easy_install. With pip, you can list all installed packages and their versions with

pip freeze

In most linux systems, you can pipe this to grep(or findstr on Windows) to find the row for the particular package you're interested in:

$ pip freeze | grep lxml

c:\> pip freeze | findstr lxml

For an individual module, you can try the __version__ attribute, however there are modules without it:

$ python -c "import requests; print(requests.__version__)"
$ python -c "import lxml; print(lxml.__version__)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
AttributeError: 'module' object has no attribute '__version__'

Lastly, as the commands in your question are prefixed with sudo, it appears you're installing to the global python environment. Strongly advise to take look into python virtual environment managers, for example virtualenvwrapper

6/16/2019 10:47:56 AM

Use pkg_resources module distributed with setuptools library. Note that the string that you pass to get_distribution method should correspond to the PyPI entry.

>>> import pkg_resources
>>> pkg_resources.get_distribution("construct").version

and if you want to run it from the command line you can do:

python -c "import pkg_resources; print(pkg_resources.get_distribution('construct').version)"

Note that the string that you pass to the get_distribution method should be the package name as registered in PyPI, not the module name that you are trying to import.

Unfortunately these aren't always the same (e.g. you do pip install memcached, but import memcache).


I think this can help but first install show package in order to run pip show then use show to find the version!

sudo pip install show
# in order to get package version execute the below command
sudo pip show YOUR_PACKAGE_NAME | grep Version

The Better way to do that is:

For the details of specific Package

pip show <package_name>

It details out the Package_name, Version, Author, Location etc.

$ pip show numpy
Name: numpy
Version: 1.13.3
Summary: NumPy: array processing for numbers, strings, records, and objects.
Author: NumPy Developers
Author-email: [email protected]
License: BSD
Location: c:\users\prowinjvm\appdata\local\programs\python\python36\lib\site-packages

For more Details: >>> pip help

pip should be updated to do this.

pip install --upgrade pip

On Windows recommend command is:

python -m pip install --upgrade pip


In python3 with brackets around print

>>> import celery
>>> print(celery.__version__)

module.__version__ is a good first thing to try, but it doesn't always work.

If you don't want to shell out, and you're using pip 8 or 9, you can still use pip.get_installed_distributions() to get versions from within Python:

update: the solution here works in pip 8 and 9, but in pip 10 the function has been moved from pip.get_installed_distributions to pip._internal.utils.misc.get_installed_distributions to explicitly indicate that it's not for external use. It's not a good idea to rely on it if you're using pip 10+.

import pip

pip.get_installed_distributions()  # -> [distribute 0.6.16 (...), ...]

    pkg.key + ': ' + pkg.version
    for pkg in pip.get_installed_distributions()
    if pkg.key in ['setuptools', 'statlib', 'construct']
] # -> nicely filtered list of ['setuptools: 3.3', ...]