Pip is the default package manager for Python. It allows you to install, upgrade, and manage third-party packages that enhance Python's functionality. This article explains how to use pip to install packages, check installations, and remove packages, with examples.
Pip stands for "Pip Installs Packages." It is a command-line tool included with Python (from version 3.4 onwards). Pip helps you download and install Python packages from the Python Package Index (PyPI).
Before installing packages, ensure pip is installed on your system.
# Check pip version pip --version
If pip is not installed, you can install it by downloading the get-pip.py script and running it with Python.
To install a package using pip, use the pip install
command followed by the package name.
requests
Package# Install the requests package pip install requests # Verify installation python -c "import requests; print(requests.__version__)"
You can install a specific version of a package by specifying the version number.
requests
# Install a specific version pip install requests==2.25.1
To upgrade an already installed package to the latest version, use the --upgrade
flag.
requests
Package# Upgrade to the latest version pip install --upgrade requests
You can view all installed packages and their versions using the pip list
command.
# List installed packages pip list
To remove a package, use the pip uninstall
command followed by the package name.
requests
Package# Uninstall the requests package pip uninstall requests
A requirements file contains a list of packages to install. This is useful for sharing dependencies in a project.
Suppose you have a file named requirements.txt
with the following content:
requests==2.25.1 numpy>=1.21.0
Install the packages using:
# Install packages from requirements.txt pip install -r requirements.txt
Pip can also install packages from URLs or local files.
# Install a package from GitHub pip install git+https://github.com/psf/requests.git
Pip simplifies package management in Python, enabling you to quickly set up the tools and libraries needed for your projects. By mastering pip, you can efficiently install, update, and remove packages to meet your development requirements.