Language: EN

python-como-usar-pip

What is and how to use pip in Python

pip is a package management system for Python. Its name comes from “Pip Installs Packages”.

With pip, we can easily install, update, and uninstall Python packages.

Packages are collections of modules and functions that can be distributed and used by other programmers.

If you want to learn more
consult the Introduction to Programming Course read more

Installing pip

In most modern Python distributions, pip is included by default. However, to ensure you have the latest version, you can follow these steps to install pip:

Check if pip is installed

Open a terminal or command line and run the following command:

pip --version

If pip is installed, you will see version information. If not, you will see an error message.

Install pip

If pip is not installed, we can install it using the following command:

On Windows systems

We download the get-pip.py and run it with Python:

python get-pip.py

On Unix/Linux or MacOS systems

sudo apt-get install python3-pip  # For Python 3

Basic usage of pip

Now that pip is installed, we can use it to manage Python packages.

Install a Package

To install a package, we use the command pip install package_name. For example, to install requests, a commonly used package for making HTTP requests in Python:

pip install requests

Uninstall a Package

If we no longer need a package, we can uninstall it with pip uninstall package_name. For example, to uninstall requests:

pip uninstall requests

List Installed Packages

We can see a list of installed packages in our environment with pip list. This will show the packages and their versions.

pip list

Update a Package

To update a package to its latest version, we use pip install --upgrade package_name.

pip install --upgrade requests

requirements.txt file

In many projects, it is common to have a requirements.txt file that lists all the packages and their versions necessary to run the project. pip can install all these packages at once from this file.

Suppose we have a requirements.txt file with the following content:

Flask==2.0.2
requests==2.26.0
numpy==1.21.2

To install all these packages, we run:

pip install -r requirements.txt

This will install the Flask, requests, and numpy packages with the versions specified in the file.