The Python Oracle

How do I check the versions of Python modules?

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Luau

--

Chapters
00:00 How Do I Check The Versions Of Python Modules?
00:19 Answer 1 Score 519
00:46 Accepted Answer Score 1124
01:50 Answer 3 Score 182
02:15 Answer 4 Score 464
03:15 Thank you

--

Full question
https://stackoverflow.com/questions/2018...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python

#avk47



ACCEPTED ANSWER

Score 1124


Use pip instead of easy_install.

With pip, list all installed packages and their versions via:

pip freeze

On 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:

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. I strongly advise to take look into Python virtual environment managers, for example virtualenvwrapper.




ANSWER 2

Score 519


You can try

>>> import statlib
>>> print statlib.__version__

>>> import construct
>>> print contruct.__version__

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 Remove importlib_metadata.version..




ANSWER 3

Score 464


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:

  1. 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).

  2. If you want to apply this solution from the command line you can do something like:

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



ANSWER 4

Score 182


Use pip show to find the version!

# In order to get the package version, execute the below command
pip show YOUR_PACKAGE_NAME | grep Version

You can use pip show YOUR_PACKAGE_NAME - which gives you all details of package. This also works in Windows.

grep Version is used in Linux to filter out the version and show it.