How to Install a Package in Python Using PIP?

Python provides various built-in and open-source packages that are used to perform basic to advanced operations. To access the open-source or third-party packages, we need to install them in Python and then import it into the program. 

To install packages in Python, mostly the “pip” package manager is used. This article will discuss the following content:

Using pip to Install a Python Package

To install a package in Python using pip, we must ensure that we have installed it in Python. Python versions 3.4 and later versions include the pip by default. But if you don’t have pip, you can use the following command in the cmd terminal to install the pip package manager:

> py -m ensurepip --upgrade

You can read this guide for installing “pip” in Windows and Linux. For Ubuntu 22.04 specifically, you can check this tutorial.

Now, you can use the following template to install any package that the “pip” package manager supports.

pip install package_name

You can find any module’s “pip” command on the official site of the Python package index. Such as for the panda’s module:

The below snippet shows the pip command for installing the panda’s version “1.5.3”.

To install the panda’s package in Python using pip, you need to open the command prompt and type the given below command:

> pip install pandas

The below snippet shows the installation of the panda’s module:

After the installation, the following command is used to verify the installation:

> pip show pandas

The below snippet shows the details of pandas module:

Using pip to Uninstall a Python Package

To uninstall a package in Python using pip you can use the below command in the command prompt terminal:

> pip uninstall pandas

The below snippet shows the uninstallation of the selected package, i.e., pandas:

How to Add Python Version to Path of Windows?

The “pip” package manager does not work in the command prompt terminal or any terminal shell if the installed version of Python is not added to the windows path. To add Python to windows Path, various methods are used in Python:

Method 1: Install Latest Python 

Install the Latest Version of Python, and during installation, tick the check box named “ADD Python.exe to Path”:

Method 2: Add Python Path Manually 

We can also add a Python file manually to the windows path. First, we need to find the installation path of our installed Python. For this, you can search the “Python.exe” in the search bar and click on the open file location option such as shown below:

After opening the installed Python location, you can copy the Python application path.

Launch the run dialog box by pressing “windows+R” and type the “sysdm.cpl” to open the system properties.

After opening the system properties now, click on the advanced tab and click the “Environment Variables”, as shown in the below snippet:

After opening the environment variable, you can add/edit or remove the path. Select the Path variable of Python and click on Edit. The next screen displays a list of all the PATH variable directories. After that, Enter the Python installation directory by clicking on New.

Now place the variable name and application path and click on the “OK” button to add Python to the path.

Conclusion

To install any package in Python using “pip”, you need to execute the “pip install package_name” command from the CMD (command prompt terminal). The installation of the Package using the pip can be verified using another pip command: “pip show package_name”. This post presented a comprehensive tutorial on how to install a Python package using pip.