note.nkmk.me

How to use pip (Install, update, uninstall packages)

Posted: 2021-10-01 / Tags: Python, pip

pip is the package installer for Python. It is used to install, update, and uninstall various Python packages (libraries).

This article describes the following basic operations of pip.

  • Install pip
  • pip and pip2, pip3
  • Details of installed package: pip show
  • List of installed packages: pip list, pip freeze
  • Install a package: pip install
    • Install from local or GitHub
  • Update a package: pip install --upgrade
    • Update pip itself
  • Uninstall a package: pip uninstall
  • Check for dependencies: pip check
Sponsored Link

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:

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 will be 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 shows 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, and to output only uptodate packages, outdated packages, packages that are not dependencies of other packages. See the following article 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. See the following article.

Sponsored Link

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 the latest version will be installed.

$ 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 article 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 version or bug-fixed version is not yet registered in PyPI, you can also 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 file 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 you run 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 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.

Sponsored Link
Share

Related Categories

Related Articles