How to check version of python modules?


How to check version of python modules?



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


construct


statlib


setuptools


# 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?


python --version



My python version is 2.7.3.


2.7.3





possible duplicate of Checking python module version at runtime
– Ciro Santilli 新疆改造中心 六四事件 法轮功
Apr 6 '14 at 15:09





Also: stackoverflow.com/questions/3524168/…
– user2314737
Dec 14 '14 at 12:27





For those interested by a command line solution, use: pip list
– KrisWebDev
Dec 21 '15 at 9:08



pip list




13 Answers
13



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 to find the row for the particular package you're interested in:


grep


$ pip freeze | grep lxml
lxml==2.3



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


__version__


$ 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


sudo





an answer below suggested pip show lxml | grep Version ; this will run much faster, since it only inspects a single package.
– Jonathan Vanasco
Dec 2 '14 at 17:01


pip show lxml | grep Version





Just for completeness: A third version is pip list | grep lxml
– 0xAffe
Jul 13 '15 at 7:45



pip list | grep lxml



You can try


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

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





Some versions of some common libraries (such as inspect) not not have a __version__ attribute, unfortunately.
– ely
Feb 26 '14 at 13:42



inspect


__version__





PySerial has serial.VERSION. Maybe there are some other commonly used modules as well, which aren't following PEP 0396: python.org/dev/peps/pep-0396
– Sussch
Nov 30 '15 at 10:15



serial.VERSION





a lot of modules do not have version
– sdaffa23fdsf
Jan 15 '16 at 23:49





@sdaffa23fdsf which modules do not have version? More than serial, inspect, PyQt and SQLite? See pycmake.
– Pål GD
Jul 11 '16 at 8:30





+1 because this works on any OS. Even if some modules do not have a version attribute, this is by far the easiest.
– RolfBly
Jul 13 '16 at 18:59



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.


pkg_resources


setuptools


get_distribution


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



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"



(Disclaimer: This is pretty much a repost of this answer, but to me it is more relevant than any other answer to this question.)





This works even if the module does not have the attribute __version__.
– imranal
Nov 10 '15 at 10:17


__version__





What about this? construct.version.full_version
– MKatleast3
Aug 7 '16 at 14:03



construct.version.full_version





Should be the top answer, it's the only reliable way of getting the package version (if there is one)
– henryJack
Jan 15 at 11:53





Looks like this plays nicely with the version you specify in setuptools's setup call. Thanks!
– Matt Messersmith
Feb 14 at 18:19


setup





Note that pkg_resources.get_distrinbution does not always work either. It issues some DistributionNotFound exception with error message like : "The 'the_package_name' distribution was not found and is required by the application"
– mjv
Apr 6 at 23:37



pkg_resources.get_distrinbution


DistributionNotFound



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


show


pip show


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





No joy here! pip: error: No command by the name pip show (maybe you meant "pip install show")
– Sam Finnigan
Apr 5 '15 at 10:46



pip: error: No command by the name pip show (maybe you meant "pip install show")





This answer is only really suitable if you need a package version from the shell. If you need it within Python, this would be a pretty bad hack. Anyways, you can use the following command to extract the version: pip show PACKAGE | awk '/^Version: / {sub("^Version: ", ""); print}'. You could probably get away with a simpler AWK script, but the aforementioned will be safer for any edge cases.
– Six
Oct 2 '15 at 12:09


pip show PACKAGE | awk '/^Version: / {sub("^Version: ", ""); print}'





@SamFinnigan pip show was implemented in pip 1.2.1.post1. You are using a terribly dated version of pip so no wonder you're having trouble! I'm currently running pip 7.1.2. If something is preventing you from updating, you can always just install it locally or in a virtualenv.
– Six
Oct 2 '15 at 12:13



pip show





has worked for me thanks. some of the packeges have not .__version__ parameter so that one is more useful.
– Salih Karagoz
Apr 13 at 6:56





sudo pip? no thanks
– D G
Jun 6 at 12:35


sudo pip



In python3 with brackets around print


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





Not every package has a __version__ attribute.
– Spedwards
Apr 15 '15 at 10:12


__version__





This answer is for python 3 - which is a different language. However, you can use this answer in python 2. To do so requires adding the line: "from future import print_function", before the other statements.
– user1976
Jun 21 '16 at 9:07





@user1976 This is valid syntax in Python 2 as well. The parentheses are simply tolerated around the argument to print, just like (2)+(3) evaluates to 5. When you have a comma inside the parentheses, things may get marginally more interesting, though for print, it still works, sort of.
– tripleee
Jul 28 '16 at 10:28



print


(2)+(3)


5


print



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


module.__version__



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:


pip.get_installed_distributions()



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+.


pip.get_installed_distributions


pip._internal.utils.misc.get_installed_distributions


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', ...]





This worked when no other solution on this page did.
– shiri
Apr 9 at 12:48





Yes, not all package creators set version, but if you're using pip, this should always work.
– waterproof
Apr 9 at 15:35


pip





Unfortunately, this solution isn't viable. Pip doesn't guarantee any in-process API, only an API through the command-line. This approach no longer works on pip 10.
– Jason R. Coombs
May 12 at 12:47





Thanks for the heads-up @JasonR.Coombs - that's too bad; I updated the answer to clarify.
– waterproof
May 13 at 17:11



The previous answers did not solve my problem, but this code did:


import sys
for name, module in sorted(sys.modules.items()):
if hasattr(module, '__version__'):
print name, module.__version__





This just avoids attempting to print __version__ if it not defined. If there is no __version__, you receive no result for the package you want.
– tripleee
Jul 28 '16 at 10:25


__version__


__version__





If the module does no have a __version__ attribute, which is the standard (python.org/dev/peps/pep-0396/#specification), it is impossible to know where and how the version is included without manual investigation.
– tashuhka
Feb 11 '17 at 15:41



__version__



The Better way to do that is:



For the details of specific Package



pip show <package_name>


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: numpy-discussion@python.org
License: BSD
Location: c:usersprowinjvmappdatalocalprogramspythonpython36libsite-packages
Requires:



For more Details: >>> pip help


>>> pip help





this is exactly what i need.
– Zuoanqh
Apr 6 at 2:34





Exactly, That's Great
– Sushant
Apr 15 at 7:03



If the above methods do not work, it is worth trying the following in python:


import modulename

modulename.version
modulename.version_info



See Get Python Tornado Version?



Note, the .version worked for me on a few others besides tornado as well.


.version



first add python, pip to your environment variables. so that you can execute your commands from command prompt. then simply give python command.
then import the package



-->import scrapy


import scrapy



then print the version name



-->print(scrapy.__version__)


print(scrapy.__version__)



This will definitely work



Some modules don't have __version__ attribute, so the easiest way is check in the terminal: pip list


__version__


pip list



To get a list of non-standard (pip) modules imported in the current module:


[{pkg.key : pkg.version} for pkg in pip.get_installed_distributions()
if pkg.key in set(sys.modules) & set(globals())]



Result:


>>> import sys, pip, nltk, bs4
>>> [{pkg.key : pkg.version} for pkg in pip.get_installed_distributions() if pkg.key in set(sys.modules) & set(globals())]
[{'pip': '9.0.1'}, {'nltk': '3.2.1'}, {'bs4': '0.0.1'}]



Note:



This code was put together from solutions both on this page and from How to list imported modules?



I had the same problem, I tried to uninstall both modules: serialand pyserial. Then I reinstalled pyserial ONLY and it worked perfectly.


serial


pyserial


pyserial






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

how to run turtle graphics in Colaboratory

Export result set on Dbeaver to CSV