Skip to content
Search

Pip

lastmod 2026-09-29created 2014-04-2955 edits

A tool for installing and managing Python packages.

Categories

How to install

You can install it easily as below.

$ sudo easy_install pip

Automatic installation

Download get-pip.py from https://pip.pypa.io/en/latest/installing.html and run it with Python.

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo python get-pip.py

If setuptools (or distribute) is not already installed, get-pip.py will install setuptools for you.

To upgrade an existing setuptools (or distribute), run

$ pip install -U setuptools

It can be done simply in one line.

curl -s -L https://bootstrap.pypa.io/get-pip.py | python

Installing from binaries

python -m pip install --only-binary wheel {opts} {packages}

Installing from source code

pip install <package> --no-binary :all: {opts} {packages}

How to upgrade

$ sudo pip install --upgrade [PACKAGE_NAME]

Manual installation

## md5=2332e6f97e75ded3bddde0ced01dbda3
$ curl -O https://pypi.python.org/packages/source/p/pip/pip-6.0.8.tar.gz
$ tar xzf pip-6.0.8.tar.gz 
$ cd pip-6.0.8
$ python setup.py install

Version specifiers

Ways to specify versions, such as ~=. See PEP 440#Version specifiers for details.

requirements.txt vs constraints.txt

See Python:Constraints#requirements.txt vs constraints.txt for details.

Download package file

$ pip download [PACKAGE_NAME]

Referencing external sites (Package Index Options)

-i, --index-url
Base URL of the Python Package Index (default https://pypi.org/simple). This should point to a repository compliant with PEP 503 (the simple repository API) or a local directory laid out in the same format.
--extra-index-url
Extra URLs of package indexes to use in addition to --index-url. Should follow the same rules as --index-url.
--no-index
Ignore package index (only looking at --find-links URLs instead)
--find-links
If a URL or path to an HTML file, then parse for links to archives such as sdist (.tar.gz) or wheel (.whl) files.
If a local path or file:// URL that is a directory, then look for archives in the directory listing.
Links to VCS project URLs are not supported.

For example, when used with pytorch:

The options above can be used at the top of a requirements.txt file.

Installing from a local directory (developer mode)

pip install --help
...
  -e, --editable <path/url>   Install a project in editable mode (i.e. setuptools "develop mode") from a local project path or a VCS url.

So you can do something like pip install -e ~/Project/avplayer.

Cache directory

The default location for the cache directory depends on the Operating System:

Unix
~/.cache/pip and it respects the XDG_CACHE_HOME directory.
macOS
~/Library/Caches/pip
Windows
<CSIDL_LOCAL_APPDATA>\pip\Cache

Command line

With pip 20.1 or later, you can find it with:

pip cache dir

Pythonic and cross-platform way

import pip
from distutils.version import LooseVersion

if LooseVersion(pip.__version__) < LooseVersion('10'):
    # older pip version
    from pip.utils.appdirs import user_cache_dir
else:
    # newer pip version
    from pip._internal.utils.appdirs import user_cache_dir

print(user_cache_dir('pip'))
print(user_cache_dir('wheel'))

How to distribute

implementation, platform, abi

I don't think there is one definitive list. You have to collect it from different sources. Start with PEP 425

  • python tag: ‘py27’, ‘cp33’
  • abi tag: ‘cp32dmu’, ‘none’
  • platform tag: ‘linux_x86_64’, ‘any’
  • --implementation:
    • cp: CPython
    • ip: IronPython
    • pp: PyPy
    • jy: Jython
  • --platform:
    • win32
    • linux_i386
    • linux_x86_64

PIP LIST in PYTHON CODE

WARNING

Calling PIP directly from code is not recommended. See the official documentation: https://pip.pypa.io/en/latest/user_guide/#using-pip-from-your-program

pkg_resources

import pkg_resources
print([p.project_name for p in pkg_resources.working_set])
# note that this is same as calling pip._vendor.pkg_resources.working_set

iter_modules

Takes a long time to execute (~300ms on computer w/ I5 CPU, SSD, & 8 gigs ram). The benefit is that it will have a far more extensive list of modules and it will output importable names.

Ex: python-dateutil is imported as dateutil, but iter_modules will give you the importable name: dateutil

from pkgutil import iter_modules
print([p.name for p in iter_modules()])

Troubleshooting

Install MacOSX

When installing on MacOSX, an error like the one below may occur.

Command "/usr/bin/python -c "import setuptools, tokenize;__file__='/private/tmp/pip-build-RRbrVj/scons/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-vQQgw8-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /private/tmp/pip-build-RRbrVj/scons

So when installing pip and other Python packages, you must use the pip -H option as below.

$ sudo -H python get-pip.py
$ sudo -H pip install scons

Operation not permitted

On Mac OSX, installing packages with something like sudo -H pip install {PACKAGE} may produce an error like the one below.

Exception:
Traceback (most recent call last):
  File "/Library/Python/2.7/site-packages/pip/basecommand.py", line 209, in main
    status = self.run(options, args)
...
OSError: [Errno 1] Operation not permitted: '/System/Library/Frameworks/Python.framework/Versions/2.7/share'

This means the directory is protected by the system. You can check it as below.

$ ls -lO /System/Library/Frameworks/Python.framework/Versions/2.7/
$ csrutil status

You can see that it is protected as restricted.

In this case, you should set things up so that Python packages are managed in a separate directory using pyenv, virtualenv, etc.

CERTIFICATE_VERIFY_FAILED

See CERTIFICATE_VERIFY_FAILED. Or choose from the list below.

  • Use HTTP instead of HTTPS (e.g. --index-url=http://pypi.python.org/simple/).
  • Use --cert <trusted.pem> or CA_BUNDLE variable to specify alternative CA bundle.
    • E.g. you can go to failing URL from web-browser and import root certificate into your system.
  • Run python -c "import ssl; print(ssl.get_default_verify_paths())" to check the current one (validate if exists).
  • OpenSSL has a pair of environments (SSL_CERT_DIR, SSL_CERT_FILE) which can be used to specify different certificate database 1.
  • Use --trusted-host<hostname> to mark the host as trusted.
  • In Python use verify=False for requests.get (see: SSL Cert Verification).
  • Use --proxy<proxy> to avoid certificate checks.
  • sudo apt-get install ca-certificates

When FileNotFoundError occurs in the check, freeze, install, list, or show commands

If you look at site-packages/pip/__main__.py, you will find the following:

# Remove '' and current working directory from the first entry
# of sys.path, if present to avoid using current directory
# in pip commands check, freeze, install, list and show,
# when invoked as python -m pip <command>
if sys.path[0] in ("", os.getcwd()):
    sys.path.pop(0)

If FileNotFoundError occurs at os.getcwd(), see Python:os#FileNotFoundError in os.getcwd.

docopt is being installed using the legacy 'setup.py install' method

  DEPRECATION: docopt is being installed using the legacy 'setup.py install' method, because it does not have a 'pyproject.toml' and the 'wheel' package is not installed. pip 23.1 will enforce this behaviour change. A possible replacement is to enable the '--use-pep517' option. Discussion can be found at https://github.com/pypa/pip/issues/8559

ModuleNotFoundError: No module named 'setuptools'

An error saying the setuptools module cannot be found occurred during a Docker build. This happens because recent Python images do not include setuptools by default.

#11 3.793 [notice] A new release of pip is available: 24.3.1 -> 25.3
#11 3.793 [notice] To update, run: pip install --upgrade pip
#11 4.057 Traceback (most recent call last):
#11 4.057 File "/app/setup.py", line 8, in <module>
#11 4.058 from setuptools import setup
#11 4.058 ModuleNotFoundError: No module named 'setuptools'

Install setuptools in the Dockerfile before running setup.py:

RUN pip install --upgrade pip setuptools wheel

See also

Favorite site

References

author Zer0 sitemap