Menu Close

Python PIP Tutorial with Examples

Python PIP Tutorial with Examples

In this article, you will learn everything about Python PIP with examples. Being a Python developer, we cannot ignore the Python PIP command because it helps us to manage the packages and their dependencies in our Python application.

Through this Python PIP tutorial, we are about to explore what is PIP, How Python PIP works, and How to install, uninstall, and upgrade Python packages with the help of the pip command with examples.

Let’s start.

What is Package in Python?

Packages are collections of the modules in Python organized in a directory. Each package contains an __init__.py file because this file marks the directory as Python packages. Python packages can also contain sub-packages and modules. The Python packages are typically published on the Python Package Index (PyPI) which is an online repository to host the Python packages. Python packages are also called Python libraries.

You cannot directly use Python Python libraries in your code, first, you will have to install Python packages in your Python environment and then you can use that module.

What is PyPI?

PyPI is a repository of Python software packages maintained by the Python community.

What is Python PIP?

PIP stands for “PIP Installs Packages”. Python PIP is a package manager for Python packages or Python Libraries. We use pip to manage the Python packages that do not come with Python by default.

How does Python PIP Work?

Before using PIP, we must know the workings of the Python PIP. Let’s see how Python PIP works.

There are some steps involved in working for the Python PIP.

When you use the ‘pip install’ command to install a Python package, pip retrieves the package from PyPI and installs it on your system.

Here are the steps:

  • Search for Package: When you execute pip install, pip first searches on the PyPI repository.
  • Download Package: Once the package is found on the PyPI, Pip downloads the package archive which contains the package code and metadata.
  • Install Package: After downloading the package in the system, PIP extracts the content of the package and installs it into the location of the Python environment. This process includes copying module modules, scripts, and other necessary files into the site-packages directory of your Python installation.
  • Dependencies: If the package has dependencies (other packages it relies on), pip also downloads and installs those dependencies recursively.

Now we have understood the workings of the PIP, It’s time to explore common commands of the PIP.

Python PIP Command

Certainly! Here’s an explanation of various pip commands along with examples:

pip install package_name

This command is used to install the specified package name from PyPI.

For example, I am going to install the Python requests package using the Python PIP command then the command would be.

pip install requests

This command installs the requests package, which is commonly used for making HTTP requests in Python. If you want to install a specific version of the Package then you can specify the version also.

As you can see in the below picture, How I am installing the Python requests module using the PIP command.

Pip Install command
pip install package_name=version_number

This command installs a specific version of a package. For example:

pip install requests==2.25.1

Installing a package from a specific source.

We can also install Python packages from a specific source like git hub.
For example, if I want to install the Python requests module from the official requests GitHub repo then will use the below command.

pip install git+https://github.com/psf/requests.git

If the package is already available in your Python environment, Then you will get the following message

Package already installed

pip install --user package_name

The --user option of the install command plays a very important role during the installation of the Python packages.

The --user option in the pip install command installs Python packages into the user’s home directory rather than the system-wide location. This can be useful when you don’t have administrative privileges on the system or when you want to install packages only for your user account, without affecting other users or the system as a whole.

When you install a package using pip install --user, the package and its dependencies are installed in the following directory:

~/.local/lib/pythonX.Y/site-packages/

In the above path, ~ represents the user’s home directory, and X.Y represents the Python version (e.g., 3.10, 3.11, etc.).

For example:

pip install --user requests

This command installs the requests package into the user’s home directory, allowing the user to use requests in their Python projects without needing administrator privileges.

pip uninstall package_name

Use this command to uninstall a previously installed Python package.

For example, I am going to install the Python requests module using the pip uninstall command, and the full command will be.

pip uninstall requests
Uninstalling package using pip

pip list

This command is used to display a list of all the installed Python packages along with their versions.

Display all the packages

pip show package_name

This command is used to show the complete package information on the console.

pip show requests
Showing package information

pip install –upgrade package_name

Upgrades a package to the latest version available on PyPI.This command upgrades the requests package to the latest version available on PyPI.

pip install --upgrade requests

pip freeze

This command is used to list all the Python packages along with their versions and also it is used to store all the packages in separate files.

To Display all the packages, issue the below command.

pip freeze

To Save all the packages in a file, issue the below command.

pip freeze with requirements file
pip freeze > requirements.txt

The above command saves all the packages along with their versions in the requirements.txt file.

pip install -r requiremens.txt

This command installs all the packages listed in the requirements.txt file.

A requirements.txt file is a text file commonly used in Python projects to specify the dependencies required by the project. Each line in the file typically represents a dependency along with its version number which allows developers to define the exact versions of packages their project depends on.

pip install -r requiremens.txt

A typical ‘requirements.txt‘ file might look like this.

requests==2.26.0
numpy==1.21.0
matplotlib==3.4.3

Note:- You can change the name of the file to something other than ‘requirements.txt‘ but the extension should be .txt which indicates, this is a plan txt file.


FAQ on Python PIP

What is PIP

Ans:- PIP is a package manager for Python programming language. it is used to install, manage, and uninstall packages in Python programming.

How do I install Python PIP?

Ans:- PIP usually comes with Python installation from Python version 3.4 onwards.

How do I upgrade Pip to the latest version?

Ans:- You can upgrade Python pip with the latest version using the pip command itself. Use this pip install --upgrade pip command to upgrade to the latest version.

How do I install a package globally and locally?

Ans:- By default, pip installs packages globally means installed packages are available for all the users for all the projects but if you want to install the Python package locally then you can use the --user option of the install command.

For example, I want to install the Python Numpy package locally, then I will use the pip install --user numpy command.

How do I create a virtual environment with Pip?

Ans:- You can create a virtual environment using the venv module, which is included with Python 3. For example: pip -m venv venv_name


Conclusion

In conclusion, we have covered all the basic commands of the Python PIP that are required for a beginner as well as Python professionals. Being a Python guy, we know at least these Python PIP commands.

To get more confidence about these PIP commands, you need to issue all commands in your systems. There are various options available in the Python PIP command, You can explore all from here.

If you found this article helpful, please share and keep visiting for further Python tutorials.

Happy Coding…

Python Data Types with Examples
Top 5 Ways to Get File Size in Python

Related Posts