meta_package_manager.managers.pip module¶

class meta_package_manager.managers.pip.Pip[source]¶

Bases: PackageManager

The pip package installer for Python, driven as a module (python -m pip) rather than through the pip executable.

Calling the module through the interpreter lets pip upgrade itself, an advantage on Windows in particular: see why you should use python -m pip.

Installed and outdated packages are read from pip’s list --format=json output. The outdated query adds --not-required to report only top-level packages, since upgrading a transitive dependency can break its parent’s version constraints (#1214). There is no search: PyPI disabled its server-side search API in 2020 under unmanageable load, so pip search no longer works (see pypa/pip#5216).

Note

All operations target the default pip scope (system site-packages, or the active virtualenv). Per-scope targeting (system vs user vs venv) and multi-binary discovery (multiple pythons via pyenv) are tracked in #1725.

Note

Interpreter discovery probes the running Python first, so an mpm installed inside a virtualenv manages that virtualenv, then the Python(s) on PATH. Two kinds are skipped so the manager only targets a scope the user can install into: mpm’s own distributor-managed bundle (Homebrew stages it under a Cellar prefix) and any externally-managed, non-virtualenv interpreter that PEP 668 forbids pip install into. When every candidate is skipped, the manager reports as unavailable.

Note

Installs, upgrades and removals are marked privileged, so a global install can escalate with --sudo, but escalation is off by default. The supply-chain cooldown needs pip 26.1, the first release to honor --uploaded-prior-to; older pip silently ignores the release-age gate.

Initialize cli_errors list.

name: str = 'Python pip'¶

Return package manager’s common name.

Default value is based on class name.

homepage_url: str | None = 'https://pip.pypa.io'¶

Home page of the project, only used in documentation for reference.

Slug of the brand mark standing for this manager in the documentation.

Names an SVG vendored under docs/assets/managers/, whose provenance and license are recorded in docs/assets/managers/logos.yaml. Inlined at the top of the manager’s page by meta_package_manager._docs; a manager leaving it unset keeps the page’s default package glyph.

Several managers legitimately share one slug, either because they wrap the same upstream (brew and cask) or because the tool has no mark of its own and its ecosystem’s stands in (apt under Debian’s swirl, cargo under Rust’s gear). Documentation-only, like homepage_url: no CLI output reads it.

platforms: frozenset[Platform] | Group | Platform | Iterable[Platform | Group] = frozenset({Platform(id='aix', name='IBM AIX'), Platform(id='almalinux', name='AlmaLinux'), Platform(id='alpine', name='Alpine Linux'), Platform(id='altlinux', name='ALT Linux'), Platform(id='amzn', name='Amazon Linux'), Platform(id='android', name='Android'), Platform(id='arch', name='Arch Linux'), Platform(id='buildroot', name='Buildroot'), Platform(id='cachyos', name='CachyOS'), Platform(id='centos', name='CentOS'), Platform(id='chromeos', name='ChromeOS'), Platform(id='clearlinux', name='Clear Linux OS'), Platform(id='cloudlinux', name='CloudLinux OS'), Platform(id='cygwin', name='Cygwin'), Platform(id='debian', name='Debian'), Platform(id='dragonfly_bsd', name='DragonFly BSD'), Platform(id='endeavouros', name='EndeavourOS'), Platform(id='exherbo', name='Exherbo Linux'), Platform(id='fedora', name='Fedora'), Platform(id='freebsd', name='FreeBSD'), Platform(id='generic_linux', name='Generic Linux'), Platform(id='gentoo', name='Gentoo Linux'), Platform(id='guix', name='Guix System'), Platform(id='haiku', name='Haiku'), Platform(id='hurd', name='GNU/Hurd'), Platform(id='ibm_powerkvm', name='IBM PowerKVM'), Platform(id='illumos', name='illumos'), Platform(id='kali', name='Kali Linux'), Platform(id='kvmibm', name='KVM for IBM z Systems'), Platform(id='linuxmint', name='Linux Mint'), Platform(id='macos', name='macOS'), Platform(id='mageia', name='Mageia'), Platform(id='mandriva', name='Mandriva Linux'), Platform(id='manjaro', name='Manjaro Linux'), Platform(id='midnightbsd', name='MidnightBSD'), Platform(id='netbsd', name='NetBSD'), Platform(id='nixos', name='NixOS'), Platform(id='nobara', name='Nobara'), Platform(id='openbsd', name='OpenBSD'), Platform(id='opensuse', name='openSUSE'), Platform(id='openwrt', name='OpenWrt'), Platform(id='oracle', name='Oracle Linux'), Platform(id='os400', name='IBM i'), Platform(id='parallels', name='Parallels'), Platform(id='pidora', name='Pidora'), Platform(id='pikaos', name='PikaOS'), Platform(id='raspbian', name='Raspbian'), Platform(id='rhel', name='RedHat Enterprise Linux'), Platform(id='rocky', name='Rocky Linux'), Platform(id='scientific', name='Scientific Linux'), Platform(id='slackware', name='Slackware'), Platform(id='sles', name='SUSE Linux Enterprise Server'), Platform(id='slitaz', name='SliTaz GNU/Linux'), Platform(id='solaris', name='Solaris'), Platform(id='sourcemage', name='Source Mage GNU/Linux'), Platform(id='sunos', name='SunOS'), Platform(id='tuxedo', name='Tuxedo OS'), Platform(id='ubuntu', name='Ubuntu'), Platform(id='ultramarine', name='Ultramarine'), Platform(id='void', name='Void Linux'), Platform(id='windows', name='Windows'), Platform(id='wsl1', name='Windows Subsystem for Linux v1'), Platform(id='wsl2', name='Windows Subsystem for Linux v2'), Platform(id='xenserver', name='XenServer')})¶

List of platforms supported by the manager.

Allows for a mishmash of platforms and groups of platforms. Will be normalized into a frozenset of Platform instances at instantiation.

requirement: str | None = '>=26.1.0'¶

26.1 is the first version to ship --uploaded-prior-to, the release-age gate mpm uses for the supply-chain cooldown (see cooldown_env_var). Older pip releases silently ignore PIP_UPLOADED_PRIOR_TO, so the floor avoids advertising a gate that does nothing.

cooldown_env_var: ClassVar[str | None] = 'PIP_UPLOADED_PRIOR_TO'¶

pip honors a release-age cooldown through its --uploaded-prior-to resolver option.

pip maps any PIP_<UPPER_SNAKE> environment variable to a config setting, so PIP_UPLOADED_PRIOR_TO sets the option without touching the user’s pip.conf. The flag excludes from resolution any distribution uploaded after the given instant, which covers install and upgrade (with transitive dependencies). pip parses the RFC 3339 timestamp produced by the default meta_package_manager.execution.CLIExecutor.cooldown_env_value().

See pypa/pip#13674.

cli_names: tuple[str, ...] = ('python3', 'python')¶

List of CLI names the package manager is known as.

This list of recognized CLI names is ordered by priority. That way we can influence the search of the right binary.

..hint::

This was helpful in the case of the Python transition from 2.x to 3.x, where multiple versions of the same executable were named python or python3.

By default, this property’s value is derived from the manager’s ID (see the MetaPackageManager.__init__ method above).

pre_args: tuple[str, ...] = ('-m', 'pip', '--no-color')¶
version_cli_options: tuple[str, ...] = ('-m', 'pip', '--no-color', '--version')¶

CLI options used to produce the version of the package manager.

The raw output produced by the package manager CLI will be parsed with the version_regexes below to extract the version number.

version_regexes: tuple[str, ...] = ('pip\\s+(?P<version>\\S+)',)¶
$ python -m pip --no-color --version
pip 2.0.2 from /usr/local/lib/python/site-packages/pip (python 3.7)
search_all_cli(cli_names, env=None)[source]¶

Yield the Python interpreters the pip manager may target.

The running interpreter is probed first, so an mpm installed into a virtualenv manages that virtualenv’s own packages, then the Python(s) found on PATH. Two kinds of interpreter are skipped, so the pip manager only ever targets a scope the user can actually install into:

  • mpm’s own distributor-managed bundle (see _running_from_bundled_app()), and

  • any externally-managed, non-virtualenv interpreter PEP 668 would forbid pip install into (see _pip_install_blocked()).

When every candidate is skipped the manager is left with no cli_path and reports as unavailable, which is correct: there is no user-managed pip environment to act on.

Todo

Evaluate findpython (the maintained MIT rewrite of pythonfinder) to replace the discovery loop here. It would only cover discovery: the eligibility filters (_running_from_bundled_app(), _pip_install_blocked()) stay mpm’s job, since findpython locates interpreters but does not judge whether pip install is allowed into one.

Return type:

Generator[Path, None, None]

property version: TokenizedString | None[source]¶

Print Python’s own version before Pip’s.

This gives much more context to the user about the environment when a Python executable is found but Pip is not.

Runs:

$ python --version --version
Python 3.10.10 (Feb  8 2023, 05:34) [Clang 14.0.0 (clang-1400.0.29.202)]
property installed: Iterator[Package]¶

Fetch installed packages.

$ python -m pip --no-color list --format=json --verbose --quiet
[
 {
    "version": "1.3",
    "name": "backports.functools-lru-cache",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": "pip"
  },
  {
    "version": "0.9999999",
    "name": "html5lib",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": "pip"
  },
  {
    "name": "setuptools",
    "version": "46.0.0",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": ""
  },
  {
    "version": "2.8",
    "name": "Jinja2",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": ""
  }
]
package_metadata_batch(packages)[source]¶

Enrich installed pip packages via importlib.metadata.

Each installed distribution exposes its METADATA file (the Core Metadata from PEP 621) plus RECORD, WHEEL, and INSTALLER files in its .dist-info directory. This method reads them in-process: no shell-outs, no network, fast enough to enumerate hundreds of distributions in a fraction of a second.

Maps Home-page / Project-URL lines into the portable homepage / vcs_url / issue_tracker_url slots, walks Requires-Dist into typed meta_package_manager.package.Dependency edges, and promotes the upstream author or maintainer to meta_package_manager.package.Originator.

Return type:

Iterator[tuple[Package, PackageMetadata]]

property outdated: Iterator[Package]¶

Fetch outdated packages.

Note

The --not-required flag filters out transitive dependencies, restricting results to top-level packages only. Upgrading transitive dependencies can break version constraints of their parent packages. See #1214.

$ python -m pip --no-color list --format=json --outdated         > --not-required --verbose --quiet
[
  {
    "latest_filetype": "wheel",
    "version": "0.7.9",
    "name": "alabaster",
    "latest_version": "0.7.10",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": "pip"
  },
  {
    "latest_filetype": "wheel",
    "version": "0.9999999",
    "name": "html5lib",
    "latest_version": "0.999999999",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": "pip"
   },
  {
    "latest_filetype": "wheel",
    "version": "2.8",
    "name": "Jinja2",
    "latest_version": "2.9.5",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": "pip"
   },
  {
    "latest_filetype": "wheel",
    "version": "0.5.3",
    "name": "mccabe",
    "latest_version": "0.6.1",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": "pip"
   },
  {
    "latest_filetype": "wheel",
    "version": "2.2.0",
    "name": "pycodestyle",
    "latest_version": "2.3.1",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": "pip"
   },
  {
    "latest_filetype": "wheel",
    "version": "2.1.3",
    "name": "Pygments",
    "latest_version": "2.2.0",
    "location": "/usr/local/lib/python3.7/site-packages",
    "installer": ""
   }
]
install(package_id, version=None)[source]¶

Install one package.

$ python -m pip --no-color install arrow
Collecting arrow
  Using cached arrow-1.1.1-py3-none-any.whl (60 kB)
Collecting python-dateutil>=2.7.0
  Using cached python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Requirement already satisfied: six>=1.5 in python3.9/site-packages (1.16.0)
Installing collected packages: python-dateutil, arrow
Successfully installed arrow-1.1.1 python-dateutil-2.8.2
Return type:

str

id: str = 'pip'¶

Package manager’s ID.

Derived by defaults from the lower-cased class name in which underscores _ are replaced by dashes -.

This ID must be unique among all package manager definitions and lower-case, as they’re used as feature flags for the mpm CLI.

upgrade_one_cli(package_id, version=None)[source]¶

Generates the CLI to upgrade the package provided as parameter.

$ python -m pip --no-color install --upgrade six
Collecting six
  Using cached six-1.15.0-py2.py3-none-any.whl (10 kB)
Installing collected packages: six
  Attempting uninstall: six
    Found existing installation: six 1.14.0
    Uninstalling six-1.14.0:
      Successfully uninstalled six-1.14.0
Successfully installed six-1.15.0
Return type:

tuple[str, ...]

Note

Pip lacks support of a proper full upgrade command. Raising an error let the parent class upgrade packages one by one.

See pypa/pip#59.

virtual: bool = False¶

Should we expose the package manager to the user?

Virtual package manager are just skeleton classes used to factorize code among managers of the same family.

remove(package_id)[source]¶

Remove one package.

$ python -m pip --no-color uninstall --yes arrow
Return type:

str

cleanup_cache()[source]¶

Removes things we don’t need anymore.

$ python -m pip --no-color cache purge
Return type:

None

doctor_cli()[source]¶

Generates the CLI running the native self-diagnosis.

check verifies that installed packages have compatible dependencies, reporting conflicts on <stdout> and exiting non-zero on any.

$ python -m pip --no-color check
No broken requirements found.
Return type:

tuple[str, ...]