pip
is the package installer for Python. It is used to install, update, and uninstall various Python packages (libraries).
Install pip
If you install Python with the standard python.org installer, pip is installed at the same time.
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 v21.2.4
The official documentation also describes how to install pips individually using ensurepip
or get-pip.py
.
But if you don’t have pip installed, it is easier to set up a new Python environment with the python.org installer unless you have a strong reason to use that old system.
On a Mac, Python 2.7 is installed by default, but pip is not included. If you install Python with Homebrew, the pip is installed at the same time.
In Anaconda, conda is used for package management instead of pip.
pip and pip2, pip3
If you have an environment where Python2 and Python3 coexist, you may be able to use the pip2
and pip3
commands in addition to the pip
command.
pip2
is the command used to manage packages used by Python2, and pip3
is the command used to manage packages used by Python3. pip
is assigned to either Python2 or Python3.
For example, note that if pip
is for Python 2, packages installed with pip
will not work with Python 3.
The usage of the command is the same for pip
, pip2
, and pip3
.
You can use the pip show
command described next to check where each package has been installed.
Details of installed package: pip show
Use pip show
to check the details of installed package.
$ pip show <package-name>
For example, the pip itself is one of the packages, so you can see the details as follows. The license and dependencies are displayed.
$ pip show pip
Name: pip
Version: 18.1
Summary: The PyPA recommended tool for installing Python packages.
Home-page: [https://pip.pypa.io/](https://pip.pypa.io/)
Author: The pip developers
Author-email: pypa-dev@groups.google.com
License: MIT
Location: /usr/local/lib/python2.7/site-packages
Requires:
Required-by:
Location
is the path where the package is actually installed.
In the example environment, the pip3
command is also available, and the following results are displayed with pip3 show
.
$ pip3 show pip
Name: pip
Version: 18.1
Summary: The PyPA recommended tool for installing Python packages.
Home-page: [https://pip.pypa.io/](https://pip.pypa.io/)
Author: The pip developers
Author-email: pypa-dev@groups.google.com
License: MIT
Location: /usr/local/lib/python3.7/site-packages
Requires:
Required-by:
These results show that the pip
command installs packages in .../python2.7/site-packages
and the pip3
command installs packages in .../python3.7/site-packages
.
Note that this is just the result of the example environment, and depending on the environment, pip
may be a command for Python3.
List of installed packages: pip list
, pip freeze
You can check the list of installed packages with pip list
.
$ pip list
Package Version
---------- -------
future 0.16.0
pip 18.1
setuptools 39.2.0
six 1.11.0
wheel 0.31.1
It is also possible to change the output format to output only up-to-date packages, outdated packages, packages that are not dependencies of other packages. See the bellow part Deep dive in pip list/freeze for details.
A similar command, pip freeze
, is also provided.
$ pip freeze
future==0.16.0
six==1.11.0
freeze
does not output pip
itself and packages for package management such as setuptools
and wheel
.
freeze
is useful to create requirements.txt
. More details in the below part.
Install a package: pip install
Use pip install
to install a package.
If a package is registered in the PyPI (the Python Package Index), you can specify its name and install the latest version.
$ pip install <package-name>
Multiple packages can be installed at the same time.
$ pip install <package-name1> <package-name2> <package-name3> ...
You can also use ==
to specify a version, such as 1.0.0
.
$ pip install <package-name>==<version>
See the following part about how to install multiple packages with the configuration file requirements.txt
.
Install from local or GitHub
The packages registered in PyPI can be installed by name only, as described above.
If the latest or bug-fixed version is not yet registered in PyPI, you can install it from your local directory or GitHub repository.
If you want to install it from local, specify the path of the directory that contains setup.py
.
$ pip install path/to/dir
You can also install it by specifying a .zip
or .whl
file with a compressed directory containing setup.py
.
$ pip install path/to/zipfile.zip
You can also install it from the Git repository.
$ pip install git+<repository-url>
Install from GitHub:
$ pip install git+https://github.com/<user-name>/<repository-name>
You can specify a branch or tag by adding @<branch-name>
at the end.
For example, the version with the v2.15.0
tag of Requests can be installed as follows.
$ pip install git+https://github.com/requests/requests@v2.15.0
The installation with git+
requires git to be installed on your system because it will be installed after git clone
.
On GitHub, you can download each version of the repository as a zip file from the release page, so you can specify the zip URL directly. In this case, you do not need to have git installed on your system.
$ pip install https://github.com/requests/requests/archive/v2.15.0.zip
Update a package: pip install --upgrade
To update installed packages to the latest version, run pip install
with the --upgrade
or -U
option.
$ pip install --upgrade <package-name>
$ pip install -U <package-name>
Update pip itself
The pip itself is also managed by pip.
If pip is not the latest version, the following message will be displayed when running the pip
command.
You are using pip version 18.0, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
As the message says, you can update the pip itself with the following command.
$ pip install --upgrade pip
For the pip2
and pip3
commands, only the first pip
should be replaced with pip2
or pip3
.
$ pip3 install --upgrade pip
Uninstall a package: pip uninstall
Use pip uninstall
to uninstall installed packages.
$ pip uninstall <package-name>
Multiple packages can be uninstalled at the same time.
$ pip uninstall <package-name1> <package-name2> <package-name3> ...
By default, you are asked before files are actually deleted, as follows.
$ pip uninstall pyflakes
Uninstalling pyflakes-2.0.0:
- Would remove:
- /usr/local/bin/pyflakes
- /usr/local/lib/python2.7/site-packages/pyflakes-2.0.0.dist-info/*
- /usr/local/lib/python2.7/site-packages/pyflakes/*
Proceed (y/n)?
Type y
to uninstall.
If you add the --yes
or -y
option to the pip uninstall
command, the confirmation is omitted.
$ pip uninstall --yes <package-name>
$ pip uninstall -y <package-name>
Check for 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 dependent package is not installed, or if it is installed but the version is not correct:
$ 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 you see such a message, you should install the corresponding package with pip install
or update it with pip install -U
.
Use pip with requirements.txt
If you are managing Python packages (libraries) with pip, you can use the configuration file requirements.txt
to install the specified packages with the specified version.
Install packages with pip: -r requirements.txt
The following command will install the packages according to the configuration file requirements.txt
.
$ pip install -r requirements.txt
You can name the configuration file whatever you like, but requirements.txt
is often used.
Put requirements.txt
in the directory where the command will be executed. If it is in another directory, specify its path like path/to/requirements.txt
.
How to write configuration file requirements.txt
An example of configuration file requirements.txt
is as follows.
###### Requirements without Version Specifiers ######
nose
nose-cov
beautifulsoup4
###### Requirements with Version Specifiers ######
docopt == 0.6.1 # Version Matching. Must be version 0.6.1
keyring >= 4.1.1 # Minimum version 4.1.1
coverage != 3.5 # Version Exclusion. Anything except version 3.5
Mopidy-Dirble ~= 1.1 # Compatible release. Same as >= 1.1, == 1.*
Like Python code, you can write comments using #
.
You can specify the version with ==
, >
, >=
, <
, <=
, etc. If the version is omitted, the latest version is installed.
Two conditions can be specified by separating them with a comma ,
. In the following example, a version of 1.0
or later and 2.0
or earlier (= 1.0 <= ver <= 2.0
) is installed.
package >= 1.0, <=2.0
Export current environment configuration file: pip freeze
pip freeze
outputs the package and its version installed in the current environment in the form of a configuration file that can be used with pip install -r
.
$ pip freeze
agate==1.6.0
agate-dbf==0.2.0
agate-excel==0.2.1
agate-sql==0.5.2
If you output pip freeze
to a file with redirect >
, you can use that file to install packages of the same version as the original environment in another environment.
First, output requirements.txt
to a file.
$ pip freeze > requirements.txt
Copy or move this requirements.txt
to another environment and install with it.
$ pip install -r requirements.txt
Deep dive in pip list/freeze
In pip, the package management system for Python, you can check the list of installed packages with pip list
and pip freeze
commands.
With pip list
, it is possible to select and output the latest version (= up-to-date) packages, non-latest version (= outdated) packages, packages that are not dependencies of other packages, etc.
pip freeze
is useful for creating requirements.txt
, which is a configuration file for installing packages in bulk.
This article does not describe all the options; see the official documentation for more details.
Difference between pip list
and pip freeze
The output results of pip list
and pip freeze
in the same environment are as follows.
$ pip list
Package Version
---------- -------
future 0.16.0
pip 18.1
setuptools 39.2.0
six 1.11.0
wheel 0.31.1
$ pip freeze
future==0.16.0
six==1.11.0
The difference between pip list
and pip freeze
is:
- The output format
- Whether the result includes packages for package management (ex.
pip
,wheel
,setuptools
)
The output format of pip list
may differ depending on the version of pip and settings, but the output format of pip freeze
is <package-name> == <version>
.
The format of pip freeze
is the format for requirements.txt
, which is a configuration file for installing packages in bulk. If you output pip freeze
as a file with redirection >
and use it in another environment, you can install the same packages at once.
$ pip freeze > requirements.txt
$ pip install -r requirements.txt
pip freeze
does not output pip
itself or packages for package management such as setuptools
andwheel
. These packages are not needed for porting the environment using requirements.txt
mentioned above.
Note that if you add the --all
option to pip freeze
, packages such as pip
will also be output.
$ pip freeze --all
future==0.16.0
pip==18.1
setuptools==39.2.0
six==1.11.0
wheel==0.31.1
As explained below, pip list
can be used to select and output the latest version (= up-to-date) packages, non-latest version (= outdated) packages, packages that are not dependencies of other packages, etc.
Therefore, you should use pip list
and pip freeze
as follows:
- If you want to check a list of packages with various conditions, use
pip list
. - If you want to create
requirements.txt
, usepip freeze
.
Select the output format: --format
In pip list
, you can select the output format with the --format
option.
$ pip list --format
<format>
can be columns
, freeze
, or json
. In pip 18.1, columns
is the default. In previous versions, the format legacy
could be specified, but not in 18.1.
$ pip list --format columns
Package Version
---------- -------
future 0.16.0
pip 18.1
setuptools 39.2.0
six 1.11.0
wheel 0.31.1
$ pip list --format freeze
future==0.16.0
pip==18.1
setuptools==39.2.0
six==1.11.0
wheel==0.31.1
$ pip list --format json
{"version": "0.16.0", "name": "future"}, {"version": "18.1", "name": "pip"}, {"version": "39.2.0", "name": "setuptools"}, {"version": "1.11.0", "name": "six"}, {"version": "0.31.1", "name": "wheel"}
List up-to-date packages: -u
, --uptodate
pip list
with -u
or --uptodate
outputs only the packages that have been updated to the latest version.
$ pip list -u
Package Version
------- -------
future 0.16.0
pip 18.1
six 1.11.0
List outdated packages: -o
, --outdated
pip list
with -o
or --outdated
outputs only updatable packages that are not the latest version.
If the output format is columns
or json
, the currently installed version and the latest version are output together.
$ pip list -o
Package Version Latest Type
---------- ------- ------ -----
setuptools 39.2.0 40.4.3 wheel
wheel 0.31.1 0.32.1 wheel
$ pip list -o --format json
{"latest_filetype": "wheel", "version": "39.2.0", "name": "setuptools", "latest_version": "40.4.3"}, {"latest_filetype": "wheel", "version": "0.31.1", "name": "wheel", "latest_version": "0.32.1"}
If the output format is freeze
, only the currently installed version is output.
$ pip list -o --format freeze
setuptools==39.2.0
wheel==0.31.1
List packages that are not dependencies of other packages: --not-required
pip list
with --not-required
outputs only packages that are not dependent on other installed packages.
$ pip list --not-required
Package Version
---------- -------
future 0.16.0
pip 18.1
setuptools 39.2.0
six 1.11.0
wheel 0.31.1
Packages output by pip list --not-required
will not break dependencies of other packages even if they are uninstalled. This is useful for finding packages that can be uninstalled in an environment where many packages are installed.
Note that only dependencies are checked, and those used as external commands, such as pip
, are also listed.