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
.
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:
Linux:
$ pip freeze | grep lxml
lxml==2.3
Windows:
c:\> pip freeze | findstr lxml
lxml==2.3
For an individual module, you can try the __version__
attribute, however there are modules without it:
$ python -c "import requests; print(requests.__version__)"
2.14.2
$ 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
You can try
>>> import statlib
>>> print statlib.__version__
>>> import construct
>>> print contruct.__version__
Update: This is the approach recommended by PEP 396. But that PEP was never accepted and has been deferred. In fact, there appears to be increasing support amongst Python core developers to recommend not including a __version__
attribute, e.g. in https://gitlab.com/python-devs/importlib_metadata/-/merge_requests/125.
Python >= 3.8:
If you're on python >=3.8
you can use a module from the built-in library for that. To check a package's version (in this example construct
) run:
>>> from importlib.metadata import version
>>> version('construct')
'4.3.1'
Python < 3.8:
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
'2.5.2'
Side notes:
-
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 dopip install memcached
, butimport memcache
). -
If you want to apply this solution from the command line you can do somthing like:
python -c \
"import pkg_resources; print(pkg_resources.get_distribution('construct').version)"
Use pip show
to find the version!
# in order to get package version execute the below command
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.
Home-page: http://www.numpy.org
Author: NumPy Developers
Author-email: [email protected]
License: BSD
Location: c:\users\prowinjvm\appdata\local\programs\python\python36\lib\site-packages
Requires:
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