Tuesday, April 19, 2016

How do I check if a particular Python package is installed or not

How do I check if a particular Python package, say mordor, is installed or not?

Firstly, check if pip (package manager) is available in your machine.
Using pip, check if the required package is installed or not.

# pip list | grep mordor
#

If mordor package is not present, I could install it using `pip install Mordor`

# pip install mordor
Collecting mordor
  Downloading mordor-1.1.tar.gz
Installing collected packages: mordor
  Running setup.py install for mordor ... done
Successfully installed mordor-1.1
#

What if my machine is not connected to the Internet?  `pip install mordor` fails for me.

# pip install mordor
Downloading/unpacking mordor
  Cannot fetch index base URL https://pypi.python.org/simple/
  Could not find any downloads that satisfy the requirement mordor
No distributions at all found for mordor
Storing complete log in /root/.pip/pip.log
#

So I have to download the tar.gz file of this package from pypi.python.org
In case of mordor package, the file I am looking for is https://pypi.python.org/packages/source/m/mordor/mordor-1.1.tar.gz

After copying the tar.gz file (mordor-1.1.tar.gz in this case) to the machine, I untar-unzip it.

# tar -zxvf mordor-1.1.tar.gz
mordor-1.1/
mordor-1.1/PKG-INFO
mordor-1.1/setup.py
mordor-1.1/src/
mordor-1.1/src/mordor.py
mordor-1.1/src/ordor.py
#

I installed the required package using `python setup.py install`

# cd mordor-1.1
# python setup.py  install
running install
running build
running build_py
creating build
creating build/lib
copying src/mordor.py -> build/lib
copying src/ordor.py -> build/lib
running install_lib
copying build/lib/mordor.py -> /usr/lib/python2.6/site-packages
copying build/lib/ordor.py -> /usr/lib/python2.6/site-packages
byte-compiling /usr/lib/python2.6/site-packages/mordor.py to mordor.pyc
byte-compiling /usr/lib/python2.6/site-packages/ordor.py to ordor.pyc
running install_egg_info
Writing /usr/lib/python2.6/site-packages/mordor-1.1-py2.6.egg-info
#
# pip list | grep mordor
mordor (1.1)
#

Here I did not talk at all about dependencies.  While installing a Python package, you may run into a situation where dependencies are missing.  So you have to first install all the dependencies, and then install the package that you wanted.

No comments:

Post a Comment