pip
I want to read about pip because I have been using Python more recently, so I think it would be helpful to know more about it.
References
- pip Documentation
- pip / python project Requirement Files
- pip command reference
Notes
pip is that package installer for Python. You can use it to install packages from the Python Package Index and other indexes.
Installation
Installation
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
Two mechanisms to install pip supported by pip's maintainers:
ensurepip
$ python -m ensurepip --upgrade # Linux
C:> py -m ensurepip --upgrade
get-pip.py
$ python get-pip.py # Linux
C:> py get-pip.py # Windows
Upgrading pip
$ python -m pip install --upgrade pip # Linux
C:> py -m pip install --upgrade pip # Windows
User Guide
Running pip
pip is a command line program. When you install pip, a pip
command is added to your system, which can be run from the prompt:
$ python -m pip <pip arguments> # Linux
C:> py -m pip <pip arguments> # Windows - executes pip using latest version of Python interpreter you have installed
Installing Packages
Install packages from:
- PyPI (and other indexes) using requirement specifiers
- VCS project urls
- Local project directories
- Local or remote source archives
pip also supports installing from requirements files
, which provide an easy way to specify a whole environment to be installed.
$ python -m pip install SomePackage # Latest Version
$ python -m pip install SomePackage=1.0.4 # Specific Version
$ python -m pip install 'SomePackage>=1.0.4' # minimum Version
pip install
has several stages:
- Identify the base requirements. The user supplied arguments are processed here.
- Resolve dependencies. What will be installed is determined here.
- Build wheels. All the dependencies that can be are built into wheels.
- Install the packages (and uninstall anything being upgraded/replaced).
When looking at the items to be installed, pip checks what type of item each is, in the following order:
- Project or archive URL
- Local directory (which must contain a
pyproject.toml
orsetup.py
, otherwise pip will report an error) - Local file
- A version specifier
pip installs dependencies before their dependents. Pip install options
Requirements Files
Requirement files
are files containing a list of items to be installing using pip installed
like so:
$ python -m pip install -r requirements.txt # Linux
C:> py pip install -r requirements.txt # Windows
Logically, a Requirements file is just a list of pip install arguments placed in a file. You should not rely on the items in the file being installed by pip in any particular order.
You can create a requirements file with the pip freeze
command:
$ python -m pip freeze > requirements.txt # Linux
C:> py -m pip freeze > requirements.txt # Windows
Requirements files can be used to force pip to install a certain version of a package.
Constraints Files
Constraint files are requirement files that only control which version of a requirement is installed, not whether it is installed or not.
$ python -m pip install -c constraints.txt
Constraints files are used for exactly the same reason as requirements files when you don't know exactly what things you want to install.
Installing From Wheels
"Wheel" is a built, archive format that can greatly speed installation compared to building and installing from source archives. pip prefers Wheels when they are available.
Uninstalling Packages
pip is able to uninstall packages like so:
$ python -m pip uninstall SomePackage
Listing Packages
To install packages:
$ python -m pip list
docutils (0.9.1)
Jinja2 (2.6)
Pygments (1.5)
Sphinx (1.1.2)
$ python -m pip show sphinx # See Detals about installed package
---
Name: Sphinx
Version: 1.1.3
Location: /my/env/lib/pythonx.x/site-packages
Requires: Pygments, Jinja2, docutils
Caching
pip provides an on-by-default caching, designed to reduce the amount of time spent on duplicate downloads and builds
What is Cached
- HTTP Responses
- The cache functions like a web browser cache.
- When making any HTTP request, pip will first check its local cache to determine if it has a suitable response to that request which has not expired.
- Locally Built Wheels
Where is the Cache Stored
pip cache dir
can be used to get the cache directory that pip is currently configured to use.
Default Paths:
$ ~/.cache/pip # Linux
C:> %LocalAppData%\pip\Cache # Windows
Cache Management
$ pip cache info # Provides an overview of pip's cache
$ pip cache remove setuptools # removes all wheel files related to setuptools from cache
$ pip cache purge # clears all files from pip's wheel and HTTP caches