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.

Date Created:

References



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:

  1. Identify the base requirements. The user supplied arguments are processed here.
  2. Resolve dependencies. What will be installed is determined here.
  3. Build wheels. All the dependencies that can be are built into wheels.
  4. 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:

  1. Project or archive URL
  2. Local directory (which must contain a pyproject.toml or setup.py, otherwise pip will report an error)
  3. Local file
  4. 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


Insert Math Markup

ESC
About Inserting Math Content
Display Style:

Embed News Content

ESC
About Embedding News Content

Embed Youtube Video

ESC
Embedding Youtube Videos

Embed TikTok Video

ESC
Embedding TikTok Videos

Embed X Post

ESC
Embedding X Posts

Embed Instagram Post

ESC
Embedding Instagram Posts

Insert Details Element

ESC

Example Output:

Summary Title
You will be able to insert content here after confirming the title of the <details> element.

Insert Table

ESC
Customization
Align:
Preview:

Insert Horizontal Rule

#000000

Preview:


Insert Chart

ESC

View Content At Different Sizes

ESC

Edit Style of Block Nodes

ESC

Edit the background color, default text color, margin, padding, and border of block nodes. Editable block nodes include paragraphs, headers, and lists.

#ffffff
#000000

Edit Selected Cells

Change the background color, vertical align, and borders of the cells in the current selection.

#ffffff
Vertical Align:
Border
#000000
Border Style:

Edit Table

ESC
Customization:
Align:

Upload Lexical State

ESC

Upload a .lexical file. If the file type matches the type of the current editor, then a preview will be shown below the file input.

Upload 3D Object

ESC

Upload Jupyter Notebook

ESC

Upload a Jupyter notebook and embed the resulting HTML in the text editor.

Insert Custom HTML

ESC

Edit Image Background Color

ESC
#ffffff

Insert Columns Layout

ESC
Column Type:

Select Code Language

ESC
Select Coding Language