PIP Tool For Easy Management Of Python Packages In Linux
Python is a very popular and powerful scripting language and has thousands of modules available which help extend its functionality. Python version that comes installed out of the box with Linux distributions has some modules pre-installed. Python modules are made freely available on the pypi website in the form of archives. PIP tool for easy Management.
In order to install a module, we could do it manually by downloading the modules from the pypi site, extract the archive and compile and install the module. This method would involve a lot of manual work and could easily become tedious when modules have other dependencies involved. PIP utility was introduced to resolve this problem. Name pip is an acronym for python indexing project.
The pip software is a command line package manager for python modules. We can use it to search for and install python modules. The best part about using pip is that it will take care of any dependencies the concerned modules have and install those dependencies automatically.
Install pip tool
To install pip on a red hat based system, we use the following command
[root@sahil-centos ~]# yum install python-pip Installed: python2-pip.noarch 0:8.1.2-5.el7 Complete!
Note: You need to have the EPEL repository enabled on your system for the above command to work because the python2-pip package resides in the EPEL repository.
To install pip on an Ubuntu system, use the following command
root@sahil-ubuntu:~# apt install python-pip
Verify that pip is installed
To verify that you have installed pip and check the version of pip installed use the following command
[root@sahil-centos ~]# pip --version pip 9.0.1 from /usr/lib/python2.7/site-packages (python 2.7)
Search for a package
We use the pip search command followed by the module name to search for it.
The pip search command does a regular expression match for the package name being searched.
So it will match the searched string as the entire package name, part of the name or an occurrence of the searched string in the package description.
Let’s look for SciPy, a popular python module used for scientific applications.
[root@sahil-centos ~]# pip search scipy apgl (0.8.1) - A fast python graph library based on numpy and scipy. austin (2016.0.1) - austin scipy package cloudml (0.4.2) - Machine learning as a service scipy-trainer coo_utils (0.1.2.1) - utilities for managing nested lists of lists of scipy.sparse matrices ---------------------------------------------------------output truncated for brevity scikits.fitting (0.6) - Framework for fitting functions to data with SciPy scikits.statsmodels (0.3.1) - Statistical computations and models for use with SciPy scipy-sugar (1.0.4) - Missing SciPy functionalities scipy-stack (0.0.5) - Helper to install the SciPy stack scipy (1.0.0) - SciPy: Scientific Library for Python scipydirect (1.3) - Python wrapper to the DIRECT algorithm scipyplot (0.0.5) - A Python Toolbox for Creating Scientific Article Figures ---------------------------------------------------------output truncated for brevity
Install a package using PIP
Use pip install command to install a module. Let’s install the scipy module we searched for earlier.
[root@sahil-centos ~]# pip install scipy Collecting scipy Downloading scipy-1.0.0-cp27-cp27mu-manylinux1_x86_64.whl (46.7MB) 100% |████████████████████████████████| 46.7MB 11kB/s Requirement already satisfied: numpy>=1.8.2 in /usr/lib64/python2.7/site-packages (from scipy) Installing collected packages: scipy Successfully installed scipy-1.0.0
Display information about a package
To display information about a python module, we use the pip show command. Let’s retrieve information about the scipy module we installed in the earlier example.
[root@sahil-centos ~]# pip show scipy Name: scipy Version: 1.0.0 Summary: SciPy: Scientific Library for Python Home-page: https://www.scipy.org Author: SciPy Developers Author-email: firstname.lastname@example.org License: BSD Location: /usr/lib64/python2.7/site-packages Requires: numpy
Uninstall a package Using PIP
To remove an installed package from the system, we use the pip uninstall command. We’ll demonstrate this by removing the scipy package we had installed earlier.
[root@sahil-centos ~]# pip uninstall scipy Uninstalling scipy-1.0.0: /usr/lib64/python2.7/site-packages/scipy-1.0.0.dist-info/DESCRIPTION.rst /usr/lib64/python2.7/site-packages/scipy-1.0.0.dist-info/INSTALLER /usr/lib64/python2.7/site-packages/scipy-1.0.0.dist-info/METADATA /usr/lib64/python2.7/site-packages/scipy-1.0.0.dist-info/RECORD --------------------------------------------------output truncated for brevity /usr/lib64/python2.7/site-packages/scipy/version.pyc Proceed (y/n)? y Successfully uninstalled scipy-1.0.0
List python packages currently installed on the system
To display all python based packages and python modules installed on the system, use the pip list command. Here’s a snippet from the output of the pip list command executed on my centos 7 system.
[root@sahil-centos ~]# pip list | tail six (1.9.0) slip (0.4.0) slip.dbus (0.4.0) targetcli-fb (2.1.fb41) targetd (0.7) urlgrabber (3.10) urllib3 (1.10.2) urwid (1.1.1) yum-langpacks (0.4.2) yum-metadata-parser (1.1.4)
Upgrade a package/module
Update an existing python package we use pip install command with the –upgrade option. Given below is an example
[root@sahil-centos ~]# pip install --upgrade pip Collecting pip Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB) 100% |████████████████████████████████| 1.3MB 149kB/s Installing collected packages: pip Found existing installation: pip 8.1.2 Uninstalling pip-8.1.2: Successfully uninstalled pip-8.1.2 Successfully installed pip-9.0.1
Installing pip without root privileges or sudo access
If you are not a system administrator designated for the server you are working on, then you would probably not be able to install pip on the system with sudo access.
But you can install pip for your own use on the system. We’ll now walk you through the step by step process of setting it up.
Step1: Download the pip software from the web
We’ll use wget to download the pip installer to our local system as shown below
[sahil@sahil-centos ~]$ wget https://bootstrap.pypa.io/get-pip.py --2017-12-10 12:30:35-- https://bootstrap.pypa.io/get-pip.py Resolving bootstrap.pypa.io... 126.96.36.199 Connecting to bootstrap.pypa.io|188.8.131.52|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 1595408 (1.5M) [text/x-python] Saving to: “get-pip.py” 100%[==================================>] 1,595,408 517K/s in 3.0s 2017-12-10 12:30:54 (517 KB/s) - “get-pip.py” saved [1595408/1595408]
Step 2: Install pip for the currently logged in user
Use the following command to install and set up pip for use by the currently logged in OS user.
[sahil@sahil-centos ~]$ python get-pip.py --user DEPRECATION: Python 2.6 is no longer supported by the Python core team, please upgrade your Python. A future version of pip will drop support for Python 2.6 Collecting pip Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB) 100% |████████████████████████████████| 1.3MB 357kB/s Collecting wheel Downloading wheel-0.30.0-py2.py3-none-any.whl (49kB) 100% |████████████████████████████████| 51kB 59kB/s Installing collected packages: pip, wheel Successfully installed pip-9.0.1 wheel-0.30.0
Step 3: Verify that pip has been installed
The previous command will create a ./local/bin directory path in your home directory. Go to that directory and you will find the pip binary available.
Verify that pip has been installed by checking the pip version.
[sahil@sahil-centos bin]$ pwd /home/sahil/.local/bin [sahil@sahil-centos bin]$ ./pip --version pip 9.0.1 from /home/sahil/.local/lib/python2.6/site-packages (python 2.6)
This confirms that our installation of the pip software has been successful. To avoid changing to the ~/.local/bin directory every time you need to use pip, you could modify the PATH variable and add the .local/bin directory to your path to allow pip to be used directly.
Edit the .bash_profile file in your home directory and update the PATH variable with the line PATH=$PATH:~/.local/bin and then source the file with the following command
We hope that you’ve found our explanation and demonstration of using the pip tool for maintaining python packages useful and will consider giving it a try. As always we look forward towards your feedback and suggestions.
Thanks for your wonderful Support and Encouragement