Introduction
Python is one of the most popular scripting languages in use today and due to its wide user base and community support, it has thousands to of modules available which help to add to its existing functionality.
The version of python that comes pre-installed with Linux distributions has some modules already available.
Modules for python are made available on the website pypi as archives.
When we need 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 involves a lot of manual work on the part of the user and would be even more difficult when the concerned modules have other dependencies involved. To solve this problem, the pip software was introduced. The name pip is an acronym for python indexing project. The pip utility is a package manager for python modules available via pypi. We can use it to install and maintain python modules on the system. When installing modules using pip, it’s will take care of all the dependencies of the modules being installed.
Install pip
To install pip on a redhat based system, we use the following command
[root@linuxnix ~]# yum install python-pip Loaded plugins: fastestmirror, langpacks base | 3.6 kB 00:00:00 epel/x86_64/metalink | 6.8 kB 00:00:00 epel | 4.7 kB 00:00:00 extras | 3.4 kB 00:00:00 puppetlabs-pc1 | 2.5 kB 00:00:00 updates | 3.4 kB 00:00:00 (1/8): base/7/x86_64/group_gz | 156 kB 00:00:01 (2/8): epel/x86_64/group_gz | 266 kB 00:00:02 (3/8): extras/7/x86_64/primary_db | 130 kB 00:00:01 epel/x86_64/updateinfo FAILED ] 431 kB/s | 5.1 MB 00:00:30 ETA (4/8): puppetlabs-pc1/x86_64/primary_db | 131 kB 00:00:13 (5/8): epel/x86_64/updateinfo | 858 kB 00:00:02 (6/8): updates/7/x86_64/primary_db | 4.5 MB 00:00:22 (7/8): base/7/x86_64/primary_db | 5.7 MB 00:01:19 (8/8): epel/x86_64/primary_db | 6.1 MB 00:03:19 Determining fastest mirrors * base: centos.mirror.net.in * epel: ftp.cuhk.edu.hk * extras: mirror.digistar.vn * updates: mirrors.123host.vn Resolving Dependencies --> Running transaction check ---> Package python2-pip.noarch 0:8.1.2-5.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================ Package Arch Version Repository Size ================================================================================ Installing: python2-pip noarch 8.1.2-5.el7 epel 1.7 M Transaction Summary ================================================================================ Install 1 Package Total download size: 1.7 M Installed size: 7.2 M Is this ok [y/d/N]: y Downloading packages: python2-pip-8.1.2-5.el7.noarch.rpm | 1.7 MB 00:00:20 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : python2-pip-8.1.2-5.el7.noarch 1/1 Verifying : python2-pip-8.1.2-5.el7.noarch 1/1 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. Please refer to our article on enabling the EPEL repository in Linux.
To install pip on an Ubuntu system, use the following command
root@linuxnix:~# apt install python-pip Reading package lists... Done Building dependency tree Reading state information... Done The following packages were automatically installed and are no longer required: linux-aws-headers-4.4.0-1032 linux-headers-4.4.0-1032-aws linux-image-4.4.0-1032-aws Use 'apt autoremove' to remove them. The following additional packages will be installed: build-essential g++ g++-5 libexpat1-dev libpython-all-dev libpython-dev libpython2.7 libpython2.7-dev libstdc++-5-dev python-all python-all-dev python-dev python-pip-whl python-pkg-resources python-setuptools python-wheel python2.7-dev Suggested packages: g++-multilib g++-5-multilib gcc-5-doc libstdc++6-5-dbg libstdc++-5-doc python-setuptools-doc The following NEW packages will be installed: build-essential g++ g++-5 libexpat1-dev libpython-all-dev libpython-dev libpython2.7 libpython2.7-dev libstdc++-5-dev python-all python-all-dev python-dev python-pip python-pip-whl python-pkg-resources python-setuptools python-wheel python2.7-dev 0 upgraded, 18 newly installed, 0 to remove and 0 not upgraded. Need to get 40.6 MB/40.7 MB of archives. After this operation, 87.9 MB of additional disk space will be used. Do you want to continue? [Y/n] y --------------------------------------------------output truncated for brevity
With pip now installed on our system, let’s go through some examples to understand how to use it.
Example 1: Check installed version of pip
To verify the installed vesion of pip, use the pip –version command.
[root@linuxnix ~]# pip --version pip 8.1.2 from /usr/lib/python2.7/site-packages (python 2.7) [root@linuxnix ~]#
Example 2: Search for a package
We use the pip search command followed by the module name to search for it.
In the below example we search for the pandas module which is commonly used for data science applications.
[root@linuxnix ~]# pip search pandas fix-yahoo-finance (0.0.19) - Temporary fix for Pandas Datareader's get_data_yahoo() pandas-msgpack (0.1.4) - Pandas interface to msgpack tableschema-pandas (0.6.1) - Generate Pandas data frames, load and extract data, based on JSON Table Schema descriptors. pyspark-pandas (0.0.7) - Tools and algorithms for pandas Dataframes distributed on pyspark. Please consider the SparklingPandas project before this one technify (0.6.3) - A framework to run technical analysis. Powered by pandas. pandas_streaming (0.1.61) - Streaming operations with pandas. -----------------------------------------------------------output truncated for brevity
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 occurnace of the searched string in the package description.
Example 3: Install a package
We use pip install command to install a module.
Let’s install the pandas module we searched for earlier.
[root@linuxnix ~]# pip install pandas Collecting pandas Downloading pandas-0.21.0-cp27-cp27mu-manylinux1_x86_64.whl (24.3MB) 100% |████████████████████████████████| 24.3MB 24kB/s Collecting numpy>=1.9.0 (from pandas) Downloading numpy-1.13.3-cp27-cp27mu-manylinux1_x86_64.whl (16.6MB) 100% |████████████████████████████████| 16.7MB 55kB/s Collecting python-dateutil (from pandas) Downloading python_dateutil-2.6.1-py2.py3-none-any.whl (194kB) 100% |████████████████████████████████| 194kB 172kB/s Collecting pytz>=2011k (from pandas) Downloading pytz-2017.3-py2.py3-none-any.whl (511kB) 100% |████████████████████████████████| 512kB 186kB/s Requirement already satisfied: six>=1.5 in /usr/lib/python2.7/site-packages (from python-dateutil->pandas) Installing collected packages: numpy, python-dateutil, pytz, pandas Successfully installed numpy-1.13.3 pandas-0.21.0 python-dateutil-2.6.1 pytz-2017.3 [root@linuxnix ~]#
Example 4: Display information about a package
To display information about a python module, we use the pip show command.
Let’s retrieve information about the pandas module we installed in the earlier example.
[root@linuxnix ~]# pip show pandas Name: pandas Version: 0.21.0 Summary: Powerful data structures for data analysis, time series, and statistics Home-page: http://pandas.pydata.org Author: The PyData Development Team Author-email: pydata@googlegroups.com License: BSD Location: /usr/lib64/python2.7/site-packages Requires: numpy, python-dateutil, pytz
Example 5: Remove a package
To remove an installed package from the system, we use the pip uninstall command.
We’ll demonstrate this by removing the pandas package we had installed earlier.
[root@linuxnix ~]# pip uninstall pandas Uninstalling pandas-0.21.0: /usr/lib64/python2.7/site-packages/pandas-0.21.0.dist-info/DESCRIPTION.rst /usr/lib64/python2.7/site-packages/pandas-0.21.0.dist-info/INSTALLER /usr/lib64/python2.7/site-packages/pandas-0.21.0.dist-info/METADATA /usr/lib64/python2.7/site-packages/pandas-0.21.0.dist-info/RECORD /usr/lib64/python2.7/site-packages/pandas-0.21.0.dist-info/WHEEL /usr/lib64/python2.7/site-packages/pandas-0.21.0.dist-info/metadata.json /usr/lib64/python2.7/site-packages/pandas-0.21.0.dist-info/top_level.txt -------------------------output truncated for brevity /usr/lib64/python2.7/site-packages/pandas/util/testing.pyc Proceed (y/n)? y Successfully uninstalled pandas-0.21.0
Example 6: List all python modules/packages installed on the system.
To display all python based packages and python modules installed on the system, use the pip list command.
[root@linuxnix ~]# pip list ansible (2.3.0.0) apache-libcloud (0.20.1) Babel (0.9.6) backports.ssl-match-hostname (3.4.0.2) chardet (2.2.1) CherryPy (3.2.2) configobj (4.7.2) configshell-fb (1.1.18) decorator (3.4.0) ecdsa (0.13) -------------------------output truncated for brevity
Example 7: Upgrade a package/module
To update an existing python package we use pip install command with the –upgrade option.
Here’s an example
[root@linuxnix ~]# 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 [root@linuxnix ~]#
Conclusion
This concludes our discussion on the pip tool for maintaining python modules on a system. In this article, we explained what the pip is and what is its purpose. We hope that after understanding the examples in this article, you’ll be able to install and use python packages with ease.
Sahil Suri
Latest posts by Sahil Suri (see all)
- Google Cloud basics: Activate Cloud Shell - May 19, 2021
- Create persistent swap partition on Azure Linux VM - May 18, 2021
- DNF, YUM and RPM package manager comparison - May 17, 2021
- Introduction to the aptitude package manager for Ubuntu - March 26, 2021
- zypper package management tool examples for managing packages on SUSE Linux - March 26, 2021