Skip to content
Fix Code Error

How to check version of python modules?

March 13, 2021 by Code Error
Posted By: Anonymous

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.

Solution

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

Answered By: Anonymous

Related Articles

  • Ubuntu apt-get unable to fetch packages
  • Error 'Map', but got one of type 'Null' flutter web with…
  • Can't install via pip because of egg_info error
  • Install pip in docker
  • Why use pip over easy_install?
  • Detect if Visual C++ Redistributable for Visual Studio 2012…
  • Error using core-scaffold from polymer JS in the latest…
  • How to install pip with Python 3?
  • Pip3 is unable to install requirements.txt during docker…
  • How to install Maven 3 on Ubuntu 18.04/17.04/16.10/16.04…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Implements vs extends: When to use? What’s the difference?

Next Post:

How do I create an Excel (.XLS and .XLSX) file in C# without installing Microsoft Office?

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • Get code errors & solutions at akashmittal.com
© 2022 Fix Code Error