Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check version of python modules?

Tags:

python

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.

like image 362
tarabyte Avatar asked Nov 24 '13 20:11

tarabyte


People also ask

How do I see what Python modules are in a package?

Inside the package, you can find your modules by directly using __loader__ of course. Show activity on this post. That only works for modules, not packages. Try it on Python's logging package to see what I mean.


2 Answers

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

like image 186
alko Avatar answered Sep 30 '22 16:09

alko


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.

like image 39
damienfrancois Avatar answered Sep 30 '22 17:09

damienfrancois