Skip to main content

PIP

What is PIP?

pip is the package installer for Python. You can use pip to install packages from the Python Package Index and other indexes.

Python Packages: A Refresher

In a computer, files are not usually all stored in the same location. Every user follows a different structure for organizing files and folders for easy access. Similar files are usually grouped together, under a common directory, as they have a similar purpose. Instead of directories, however, libraries and methods are organized using what we call modules and packages. This is another method of file organization.

A Python module is a collection of methods and properties which share a common purpose. A package further groups similar modules together to present an independent ecosystem of code that can help solve a bigger problem. More often than not, projects require traditional algorithms to be followed to complete routine tasks like data sorting, searching, or data manipulation. Writing code for these use-cases inside your project can increase clutter, as the focus can shift from your business logic to your sorting and searching methods.

To address this problem, code for such regular activities is typically put as distinct modules, which helps to reduce clutter in the main project while also allowing for better testing and individual development. With package distribution platforms like the Python Package Index, these packages may subsequently be exported and made available to a large number of developers. Then you may use pip to install a package.

Python pip Tool

Installing Packages

The pip tool can be used to install packages available over the Python Package Index via the command line. pip comes preinstalled with most modern versions of Python. You need to put the name of the package that you wish to install in your project in the following syntax:

pip install package_name

As you can see, the above command is composed of three parts:

  • pip: Instructs the terminal to invoke the pip tool and pass the trailing tokens as arguments to it.

  • install: Instructs the pip tool that a package needs to be installed.

  • package_name: Denotes the name of the package that has to be installed.

Here’s how you can use this syntax to install the popular statistical library Numpy:

pip install numpy

If you have worked with npm, used for JavaScript development, the syntax is nearly the same:

npm install package_name

The tool looks for the package by name in the PyPI (Python Package Index) and installs the package if it is available.

You can also install multiple packages at once, using the following syntax:

pip install package1 package2 package3 ...

This is how you would use this syntax in a real-life scenario:

pip install numpy pandas

Using the requirements.txt File

The above method can get quite cumbersome if you have a long list of packages to install. pip offers a way of installing packages using a requirements.txt file. This is how a usual requirements.txt file looks:

###### Packages that are not version-specific
numpy
pandas
beautiful-soup

###### Packages that require certain versions to be installed
docopt == 0.6.1. # When looking for the exact version
keyring >= 4.1.1. # When looking for any version above the
specified one
Mopidy-Dirble ~= 1.1. # When looking for a compatible release.
Equivalent to >= 1.1or == 1.*

And this is how the syntax of a pip call with a requirements.txt file looks like:

pip install -r requirements.txt

Uninstalling Packages

Many times, it is necessary to remove packages from your projects for maintenance or upgrading purposes. The pip tool can be used to remove packages from your project as well:

pip uninstall package_name

As you can see, the above command is composed of three tokens:

  • pip: Instructs the terminal to invoke the pip tool and pass the trailing tokens as arguments to it.

  • uninstall: Instructs the pip tool that an installed package needs to be removed.

  • package_name: Denotes the name of the package that has to be removed.

You can use the above syntax to uninstall any library, such as NumPy, like this:

pip uninstall numpy

Once again, this syntax is similar to that of npm’s JavaScript package library:

npm uninstall package_name

For uninstalling multiple packages, you can follow this syntax:

pip uninstall package1 package2 package3 ...

This is how it would be used in a real-life scenario:

pip uninstall numpy pandas

You can also use the requirements.txt file to uninstall a list of packages at once:

pip uninstall -r requirements.txt

Viewing All Installed Packages

Very often, you will need to view the list of packages that are installed in your project locally. Doing so helps you to identify if a package has been installed before, and helps you check if a certain version of a package is present in your project. The pip list command shows a list of all packages in your project. Here’s how you can use the command:

pip list

This will return an output similar to this:

docutils (0.10)
Jinja2 (2.7.2)
# ...and so on

Alternatively, you can pass an outdated option to retrieve a list of all packages with their current and latest available versions. This helps to identify which packages need to be updated. This is how the syntax looks like:

pip list --outdated

This is how a usual output would look like:

docutils (Current 0.10 Latest: 0.11)
Sphinx (Current: 1.2.1 Latest 1.2.2)

Viewing Details for an Installed Package

The pip show command helps you to view the details of an installed package in your Python project. It returns details like version, author, license, and dependencies for the said package. Its syntax is simple:

pip show sphinx

This is how a sample output would look like:

Name: Sphinx
Version: 1.4.5
Summary: Python documentation generator
Home-page: http://sphinx-doc.org/
Author: Georg Brandl
Author-email: georg@python.org
License: BSD
Location: /my/env/lib/python2.7/site-packages
Requires: docutils, snowballstemmer, alabaster, Pygments, imagesize, Jinja2, babel, six

Freezing Package Versions

An important feature that all package managers need to have is the ability to freeze packages to their set versions. This is important when the project is being developed on multiple systems, and the same version of packages is required across all of them to maintain the stability of the project.

While you can list out the installed packages with their versions with the list command, you will need to manually format them according to the requirements.txt file format that we saw earlier if you want to install these dependencies quickly on another system.

The pip freeze command does this for you without any hassle. It prints the list of installed packages in the requirements format, and you can combine it with the output redirection operator of the command line to save it to a file. This is how the complete syntax would look like:

pip freeze > filename

You can directly create a ready-to-go requirements.txt file by using this command:

pip freeze > requirements.txt

Now you can put this requirements.txt file in the project in which you want to install these packages. We have already seen an example above on how to use such a file to install packages at once.

pip vs pip3

Since Python 2 version 2.7.9 and Python 3 version 3.4, the standard Python installation includes a pip installation. However, when Python was upgraded from version 2 to 3, pip was also upgraded to pip3. When Python 3 was released, the old pip was replaced with a soft connection to the new pip3. This implies that you are actually invoking pip3 when you run the old pip on your command line with the new Python3 installed on your system.

If your system has both Python 2 and Python 3, the pip command will run the pip installer associated with your Python 2 installation, while the pip3 command will run the pip3 installer associated with your Python 3 installation. Both pip and pip3 have the same syntax; all you have to do is replace pip with pip3. Here's how to use pip3 to install NumPy:

pip3 install numpy

All other commands work similarly.


Conclusion

The Python pip tool contains a list of commands to help you manage your packages efficiently. install and uninstall commands can be used to add or remove packages easily. list and freeze can be used to view the list of installed packages in raw/formatted manner. show can be used to view all details of one package at once.

This tutorial discussed, with reference to syntax and examples, the basics of PyPI packages, and how to use them with the pip tool. Now you are ready to start using the official Python package manager like a Python pro!


External References