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 about Package Managers
check the Introduction to Programming Course read more
Installing pip
In most modern Python distributions, pip
is already included by default. However, to ensure you have the latest version, you can follow these steps to install pip
:
Verify if pip is installed
Open a terminal or command line and run the following command:
pip --version
If pip
is installed, you will see information about the version. If not, you will see an error message.
Install pip
If pip
is not installed, we can install it using the following command:
Download the get-pip.py and run it with Python:
python get-pip.py
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 the 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 needed 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.