What is PIP in Python?

Challenge Inside! : Find out where you stand! Try quiz, solve problems & win rewards!

Learn via video course

Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
By Rahul Janghu
Free
star4.90
Enrolled: 1000
Python Certification Course: Master the essentials
Python Certification Course: Master the essentials
Rahul Janghu
Free
4.90
icon_usercirclecheck-01Enrolled: 1000
Start Learning

Abstract

Like any other programming languages, Python too relies on other 3rd party libraries or packages using which can greatly simplify our lives. But, the major objective of this tutorial is, how can we install them into our system to further use it?

For that, the Python developers use the special tool - PIP in Python. It seamlessly handles the downloading, installing and managing of theses packages.

So, let us decode bit by bit what is pip in python and cover all the major aspects of it.

Introduction to PIP in Python

We often come across situations when we need to write some code which is already written by someone else, and ideally can solves the problem for us.We can just directly use that code, instead of writing it from the scratch. Doing this saves tons of time, and provide us the most optimised version of the code. Simply, we need not re-invent the wheel.

This is where pip comes into play. Let us now see what exactly is PIP in Python.

What is PIP in Python?

PIP is the package manager for python. In other words, it is a tool that allows us to install the python packages and dependencies(the software components required by your code to work without throwing any errors) which are not automatically provided to us by the Python's Standard Library.

Pip has become the standard package manager for Python.

Pip can download, install and manage packages in Python very efficiently and in no time!

Now you must be wondering, what exactly is a Package Manager and what does it exactly do?

Package Manager

A package manager is a programming language’s tool which helps us to install any external dependencies very easily. It seamlessly handles installing and uninstalling any package.

Exg: JavaScript uses npm for package management, Python uses pip, and .NET use NuGet.

Package Management in Python has become so vastly popular and important that it is by default included with the Python installer, since versions 3.4 for Python3, and 2.7.9 for Python2.

Let us now see, how to get pip installed in our system.

How to Install PIP in Python

If you are using latest Python version, then pip comes pre-installed with it.

Usually pip is automatically installed in your system if you are:

You can verify if pip is available by running the following command in your console:

If you get an output like this -

displaying the current pip version, and the location and version of Python, then, pip is installed in your system.

Otherwise, you can download & install pip from here - pypi.org

There are 2 ways to install pip:

  1. ensurepip
  2. get-pip.py

As we move ahead the tutorial, we will discuss in further details how to install pip in different platforms.

How to Use PIP in Python?

pip in Python operates from the command line or from a shell. Once we have installed pip, we can directly use it along with some arguments. The basic syntax of pip in python is -

py -m pip executes pip using the latest Python interpreter you have installed. So, here we can type "pip" followed by some arguments like install some package --

Example:

Let us now see the various usage of pip in Python --

1. Basic Package Installations

We use the install command to install any package using pip in python.Basic syntax is:

For example,let us install requests using pip - we will enter the following command:

After this, it will give an output similar to:

Thereafter, the package will be installed and you may import it and use in your code easily.

  • Usually pip installs the package in the Python installation folder : <installation_folder_name>\python39\lib\site-packages.

  • pip when used with virtualenv will generally install packages in the path --<virtualenv_name>/lib/<python_ver>/site-packages

But anyway, you can always check the installation path of your packages using --

Example:

Output: what is pip in python

2. Specifying Package Version with pip

Using the pip install command in Python always installs the latest version of a package. To install a specific version of the package, instead of the latest one, we may specify the version directly with the pip command.
Syntax:

It will install the package with the mentioned version.

For example:

Output:

The above command will download and install the package named ‘sciPy’ of the version specified by us(and not the latest one). It will install it in your python installation folder or if you are using virtual environment, then here -

<virtualenv_name>/lib/<python_ver>/site-packages

3. Displaying Package information with pip

The pip show command displays the complete information about a package installed using pip in Python.

Suppose, we install the python package requests through the command -

pip install requests

Now to check the package's complete information we can use the pip show command.

Run the below command:

We will get a detailed summary of the package -

This command majorly helps us in knowing the location of a package, it's version, requirements/dependencies and many more.

4. List Installed Packages with PIP

We can use the pip list command to list all the packages installed in our system or the current python environment.

Run this into your terminal:

We will get the list of available packages:

5. Uninstall Packages with PIP

We can uninstall a package using pip with the pip uninstall command.For example:

It will uninstall sciPy from the current python environment:

Note: The packages which were installed as dependencies with this package will not be removed. For example, "numpy" will not be uninstalled(which is a dependency for sciPy).

6. Search Packages with PIP

Earlier, we could search for any package using pip in Python through the pip search command. But unfortunately pip search is now permanently banned by python.org (you can check the link to know more).

They were experiencing "hundreds of thousands of search calls per hour" for 100 days(since Nov 14, 2020), and the XMLRPC API, through which these search calls were made, is already deprecated.

Note: We may search for any package directly on pypi.org .

7. Listing Additional Packages with PIP

The Python pip freeze command is used to list down all the packages that we have installed in our system. It basically tells us:

  • The modules/packages we have installed using pip install in the current environment
  • The versions of these modules currently installed in our system.

Run the below command in your console:

Output:

The freeze command actually dumps all the packages and their versions to into the standard output.

8. Using Requirement Files with PIP

Firstly, let's understand the purpose of requirements.txt file in a code --

Basically, we generate and share the requirements.txt files to make it easier for other developers to install the correct versions of the required libraries in python (or “packages”) to run the Python code we have written.

To generate the requirements.txt file, we can simply run this command --

After executing this code, it will generate a requirements.txt file into the working directory.

Now, anyone can simply run the below command to get all dependencies installed into their system

Once these dependencies are installed, then you are good to go!

9. Listing Outdated Packages with PIP

We can use the pip list --outdated command to list all the packages which are outdated in python.

Example:

Then it will output the packages along with their current & latest version available --

PackageVersionLatestType
beautifulsoup44.9.34.10.0wheel
certifi2021.5.302021.10.8wheel
idna2.103.3wheel
pip20.2.321.3.1wheel
pyodbc4.0.314.0.32wheel
selenium3.141.04.0.0wheel
setuptools49.2.159.1.1wheel
soupsieve2.2.12.3.1wheel
urllib31.26.61.26.7wheel

After having seen this major usage, let us now see how to install pip in different platforms.

Installing PIP:

Let us check the various ways to install pip --

How to Install PIP on Windows

To install pip in Windows follow the given steps:

  1. Download the get-pip.py installer script.
  2. You can simply Right click on the link -> save as; and save it inside your local machine in any specific folder.
  3. Naigate to a terminal/command prompt, cd to the folder containing the get-pip.py file
  4. Run the below command

This should install pip in your system. Know more here : pypa/get-pip.

Note: get-pip.py is a Python script that uses some bootstrapping logic to install pip.

How to Install PIP on Mac

You can simply follow the above mentioned steps to install pip on Mac using the get-pip.py --

This should install pip in your system. You can know more about this script in the pypa/get-pip readMe

Although an alternative way to install pip is through sudo / brew:

  1. Run the following command into your terminal:

Or through brew,

This will install the latest version of Python, with pip already installed in it. If not, then you can re-link Python using the following Terminal command:

How to Install PIP on Linux

If you have python already installed in your Linux system,you should be able to install pip using your system's package manager.

Although you can use the get-pip.py script to install pip, but given below is a more preferable way.

1. Advanced Package Tool :

  • Python 2.x
  • Python 3.x

2. Pacman Package Manager:

  • Python 2.x
  • Python 3.x

3. Yum Package Manager:

  • Python 2.x
  • Python 3.x

This should install pip in your system.

How to Install PIP on Raspberry Pi

If you are using Raspberry Jessie then pip comes installed by default with it. If you are using an older version, anyway you can install pip with the following command --

  • Python 2.x:
  • Python 3.x:

How to Upgrade pip for Python

Upgrading versions is important to mitigate vulnerabilities. Also, there are frequent fixes to bugs, and security holes with every update.

pip in Python can be upgraded by:

For windows:

For Linux/Mac/Raspberry Pi:

Note: For some versions of Linux and Raspberry Pi, you may need to use pip3 for the installation.

Downgrading Packages with PIP

Downgrading pip in Python is simple. But, you must specify the version into which you are downgrading your pip to.

Enter the following command into your cmd:

Mention a version of your choice while downgrading. It will downgrade to that version.

Alternatives to PIP

There are several alternatives to pip which are worth trying. Let us dive bit more and learn about few of them.

  • Conda Conda is an open source package manager.
  1. It helps helps you find and install python packages and their dependencies very easily.
  2. The conda package is included in Anaconda.
  • Pipenv Pipenv : It aims to bring the best of all packaging worlds to Python.
  1. Merges virtual environment and package management in a single tool.
  2. It automatically adds/removes packages from our Pipfile as we install/uninstall packages.
  • Poetry Poetry : It simplifies package version management even more.
  1. Poetry helps you declare, manage and install dependencies of Python projects
  2. It supports Python 3.6+.

Conclusion

So, finally we came to the end of this tutorial with a brief idea of pip. Let us just recap what we learnt throughout the tutorial in a nutshell --

  • Pip is a Package manager, used to install/manage package/dependencies.
  • Python's extensive standard library is published to Python Package Index (PyPI) and pip allows developers to install them in their environment.
  • We also learnt about installation of pip in different OS (windows/Mac/Linux/Raspberry).
  • We have seen installing/ uninstalling/ listing/ upgrading/ downgrading packages with pip in Python
  • Finally, we saw some alternatives of pip in Python.

Read More:

  1. How to Install Python on Windows?
  2. How to Install Python on Linux?
  3. How to Install Python on MacOS?