How to use pip (Install, update, uninstall packages)
Pip is the Python package installer, used to install, update, and uninstall packages (libraries).
This article explains how to use pip.
Install pip
When installing Python using the standard installer from python.org, pip is also installed simultaneously.
Usually, pip is automatically installed if you are:
- working in a virtual environment
- using Python downloaded from python.org
- using Python that has not been modified by a redistributor to remove ensurepip
Installation - pip documentation v24.0
The official documentation provides instructions for individually installing pip using ensurepip
or get-pip.py
.
However, if pip is not installed, setting up a new Python environment with the python.org installer is usually the easiest approach, unless you specifically require an older system.
On Mac, pip is also installed simultaneously when installing Python via Homebrew.
Note that, in the Anaconda environment, conda is used for package management instead of pip.
pip, pip2, and pip3
In environments where Python2 and Python3 coexist, pip2
and pip3
commands may be available alongside pip
.
pip2
manages Python2 packages, while pip3
manages Python3 packages. pip
is assigned to either Python2 or Python3. For example, if pip
corresponds to Python2, packages installed with pip
will not work in Python3.
The command usage is the same for pip
, pip2
, and pip3
.
You can check where each package is installed using pip show
described later.
Install packages: pip install
Use pip install
to install packages.
Packages registered on PyPI (the Python Package Index) can be installed in their latest version by simply specifying their name.
$ pip install <package-name>
It is possible to install multiple packages at once.
$ pip install <package-name1> <package-name2> <package-name3> ...
You can also specify a version, such as 1.0.0
, using ==
.
$ pip install <package-name>==<version>
For methods on installing packages in bulk using requirements.txt
, refer to the following article.
Install packages from local or GitHub
If the latest or bugfix version has not yet been registered on PyPI, you can install packages from a local directory or a GitHub repository.
To install packages from a local source, specify the path to the directory containing setup.py
.
$ pip install path/to/dir
It is also possible to install packages from compressed zip
or whl
files that contain setup.py
.
$ pip install path/to/zipfile.zip
You can also install packages from a Git repository using the git+
prefix followed by the repository URL.
$ pip install git+<repository-url>
For example, to install packages from GitHub:
$ pip install git+https://github.com/<user-name>/<repository-name>
Appending @<branch-name>
at the end allows installation from a specific branch or tag. For example, to install the v2.30.0
tagged version of Requests:
$ pip install git+https://github.com/requests/requests@v2.30.0
Note that installing directly from a Git repository with git+
requires having Git installed on your system, as this method performs a git clone
.
On GitHub, each version of the repository can be downloaded as a zip file from the release page, allowing you to specify the zip URL directly. In this case, you do not need to install Git on your system.
$ pip install https://github.com/requests/requests/archive/v2.30.0.zip
Update packages: pip install --upgrade
To update installed packages to their latest versions, use the pip install
command with the --upgrade
or -U
option.
$ pip install --upgrade <package-name>
$ pip install -U <package-name>
Update pip itself
Pip itself is also managed through pip. Like other packages, you can update pip itself to the latest version using the following command.
$ pip install --upgrade pip
For pip2
or pip3
commands, replace the first pip
with pip2
or pip3
.
$ pip3 install --upgrade pip
Uninstall packages: pip uninstall
Use pip uninstall
to uninstall packages.
$ pip uninstall <package-name>
Multiple packages can be uninstalled at the same time.
$ pip uninstall <package-name1> <package-name2> <package-name3> ...
By default, the system asks for confirmation before actually removing the package.
$ pip uninstall pyflakes
Found existing installation: pyflakes 3.0.1
Uninstalling pyflakes-3.0.1:
Would remove:
/opt/homebrew/bin/pyflakes
/opt/homebrew/lib/python3.11/site-packages/pyflakes-3.0.1.dist-info/*
/opt/homebrew/lib/python3.11/site-packages/pyflakes/*
Proceed (y/n)?
Type y
to uninstall.
To uninstall without a confirmation prompt, use the --yes
or -y
option.
$ pip uninstall --yes <package-name>
$ pip uninstall -y <package-name>
Check details of installed packages: pip show
Use pip show
to check details of installed packages.
$ pip show <package-name>
For example, pip itself is one of the packages, so its details can be checked as follows. Information such as the license and dependencies will be displayed.
$ pip show pip
Name: pip
Version: 23.2.1
Summary: The PyPA recommended tool for installing Python packages.
Home-page: https://pip.pypa.io/
Author: The pip developers
Author-email: distutils-sig@python.org
License: MIT
Location: /opt/homebrew/lib/python3.11/site-packages
Requires:
Required-by:
Location
shows the actual installation path of the package. In the example environment, the pip
command is for Python3, and packages are installed in /opt/homebrew/lib/python3.11/site-packages
.
List installed packages: pip list
, pip freeze
You can list installed packages using pip list
.
$ pip list
Package Version
---------- -------
pip 23.2.1
setuptools 68.0.0
six 1.16.0
wheel 0.40.0
It is also possible to change the output format to show only up-to-date packages, outdated packages, and packages that are not dependencies of other packages. See the following article for details.
A similar command, pip freeze
, is also provided.
$ pip freeze
six==1.16.0
Unlike pip list
, pip freeze
excludes package management tools like pip
itself, setuptools
, and wheel
from its output. It is used to create requirements.txt
. See the following article.
Check dependencies: pip check
You can use pip check
to verify that installed packages have compatible dependencies.
If everything is fine:
$ pip check
No broken requirements found.
If a required package is not installed or is installed but an outdated version:
$ pip check
pyramid 1.5.2 requires WebOb, which is not installed.
$ pip check
pyramid 1.5.2 has requirement WebOb>=1.3.1, but you have WebOb 0.8.
If such messages are displayed, you should install the corresponding package with pip install
or update it with pip install -U
.