Pip
lastmod 2026-09-29created 2014-04-2955 edits
A tool for installing and managing Python packages.
Categories
- pip
- Python:Requirements - About the requirements.txt file used when installing with Python's pip
- Python:Constraints - About the constraints.txt file used when installing with Python's pip
- PEP425 - Compatibility Tags for Built Distributions
- PEP440 - Version Identification and Dependency Specification
- Python:importlib - importlib module
- Python:distutils - distutils module
- Python:pkgutil - pkgutil module
- Python:Module
How to install
You can install it easily as below.
Automatic installation
Download get-pip.py from https://pip.pypa.io/en/latest/installing.html and run it with Python.
If setuptools (or distribute) is not already installed, get-pip.py will install setuptools for you.
To upgrade an existing setuptools (or distribute), run
It can be done simply in one line.
Installing from binaries
Installing from source code
How to upgrade
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
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-linksURLs 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:
-
--index-urlhttps://download.pytorch.org/whl/cu117 -
--find-linkshttps://download.pytorch.org/whl/torch_stable.html
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/pipand it respects theXDG_CACHE_HOMEdirectory.
- macOS
-
~/Library/Caches/pip
- Windows
- <CSIDL_LOCAL_APPDATA>
\pip\Cache
Command line
With pip 20.1 or later, you can find it with:
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
- How to share your Python packages
- Distributing packages with PyPI: your own modules can be downloaded with pip too!
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
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.
Operation not permitted
- Stackoverflow: OSX El Capitan: sudo pip install OSError: Errno: 1 Operation not permitted
- How to use pip after the El Capitan Max OS X upgrade?
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.
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>orCA_BUNDLEvariable to specify alternative CA bundle.- E.g. you can go to failing URL from web-browser and import root certificate into your system.
python -c "import ssl; print(ssl.get_default_verify_paths())" to check the current one (validate if exists). SSL_CERT_DIR, SSL_CERT_FILE) which can be used to specify different certificate database 1. --trusted-host<hostname> to mark the host as trusted. verify=False for requests.get (see: SSL Cert Verification). --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:
See also
Favorite site
- pip website
- Installing Packages — Python Packaging User Guide
- The PIP package manager
- Python package management
- Perspectives on installing Python packages: easy_install, pip, distutils
- User Guide - pip documentation v20.3.2
References
- Cartopy
- Conda
- Data Version Control
- Devpi
- Elasticsearch:Python
- Helm
- Kubernetes
- Maskrcnn-benchmark
- OpenCV-Python
- Package management system
- Pipenv
- Poetry
- Pointcept:Arm64
- Pyenv
- Pypiserver
- Python
- Python Package Index
- Python:Constraints
- Python:Isolation
- Python:Libraries
- Python:Requirements
- QuickTile
- Rye
- SciPy
- Semshi
- Setuptools
- Trac:HowToInstall
- Vim:Plugin:Pyclewn
- WebProgramming:Beginner
- Youtube-dl