What is PIP in Python?

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

This article will explore PIP in Python, covering its installation, upgrading, and uninstallation of packages. But first, let's gain a clear understanding of what Python PIP is.

Python Packages

Python package, a collection of Python code encompassing one or more modules or libraries. These packages are commonly available on the Python Package Index (PyPI) and can be conveniently installed via pip. You might find modules, sub-packages, and various supplementary resources like documentation and data files within a Python package.

What is Python PIP?

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!

What exactly is a Package Manager, and what does it do?

Package Manager

A package manager is a programming language’s tool that 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 essential 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 Python PIP?

If you use the latest Python version, 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 -

Showing the current pip version and the location and version of Python, 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 proceed with the tutorial, we will discuss in further detail how to install pip on different platforms.

How to Install Package with Python PIP?

We use the install command to install any package using pip in python. The 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:

After that, the package will be installed, and you may import it and use it 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 mostly install package 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

Specifying Package Version with PIP

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

It will install the package with the mentioned version.

For example:

Output:

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

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

Display package information using pip

The pip show command displays the complete details about a package installed by pip in Python.

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

pip install requests

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 know the location of a package, its version, requirements/dependencies, and many more.

Using Python PIP to Get a List of Locally Installed Python Modules

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:

Python PIP to Uninstall Packages

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 dependent packages installed alongside this package will remain intact and won't be uninstalled. For example, "numpy" will not be uninstalled(a dependency for sciPy).

Searching Packages with Python PIP

Earlier, we could search for any package using pip in Python by 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 .

How to Use Requirement Files with Python

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

We generate and share the requirements.txt files to make it easier for 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 run this command --

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

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

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

Using Python PIP to List Additional Packages

The Python pip freeze command lists all the packages we have installed in our system. It tells us:

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

Run the below command in your console:

Output:

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

Outdated Packages with Python PIP

We can use the pip list --outdated command to list all the outdated packages 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 seeing this major usage, let us see how to install pip on different platforms.

Upgrading Packages

Upgrading packages in Python is essential for maintaining the health and security of your Python environment. Regular upgrades ensure that you have the latest features and fixes. The Python package manager, pip, makes upgrading straightforward.

Why Upgrade?

  • Access to New Features: New versions of packages often come with additional functionalities and improvements.
  • Security Updates: Regular updates can patch vulnerabilities found in older versions.
  • Performance Enhancements: Updated packages may include optimizations that make your code run faster and more efficiently.

How to Upgrade Packages

  1. Upgrade pip Itself: It's good practice to start by upgrading pip to ensure you have the latest version. This can be done with the command:

  2. Upgrade a Specific Package: To upgrade an individual package, use the following command:

    Replace [package_name] with the actual name of the package you wish to upgrade.

  3. Upgrade All Packages: Although pip does not have a built-in command to upgrade all packages at once, you can use the following command to list all outdated packages and then upgrade them individually:

    This command displays a list of all packages that have newer versions available. You can then upgrade each package using the command mentioned in step 2.

Upgrading packages in Python is a straightforward process that can significantly impact your applications' functionality, security, and performance. Regularly updating your environment ensures you work with current tools and libraries.

Downgrading Packages

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

Enter the following command into your cmd:

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

Conclusion

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

  • Pip is a Package manager used to install/manage packages/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 learned about pip installation 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.
  1. How to Install Python on Windows?
  2. How to Install Python on Linux?
  3. How to Install Python on MacOS?