Closed
Description
What's the problem this feature will solve?
Currently, pip uses a vendored version of pkg_resources
in several places.
pkg_resources has a few downsides for our use cases, including:
- unconditional scan of entire
sys.path
on import (Avoid full path enumeration on import of setuptools or pkg_resources? setuptools#510), when we may only need it for a subset of those directories - various issues filed regarding corrupt
METADATA
files in site-packages preventing pip from starting - complicated to vendor vs a standalone library, since we need to extract it from setuptools
- higher barrier of entry for other package management tools that want pip-compatible behavior
Describe the solution you'd like
Replace pkg_resources
with importlib.metadata
(via the importlib-metadata
backport for Python < 3.8).
Our current usages for pkg_resources
are:
build_env.BuildEnvironment
: identify the set of packages available/conflicting in the isolated environment directoriesreq.req_install.InstallRequirement
: locate an already-installed packagecommands.search
,commands.show
: iterate over the set of installed packages and access metadatawheel
:- parse entrypoints
- check wheel version of unpacked Wheel
- ad-hoc usage of parsing/canonicalization functions
A combination of importlib_metadata
and packaging
can satisfy these requirements:
importlib.metadata.distributions(path=["dir1", "dir2"])
then comparison of the version usingpackaging.specifiers.SpecifierSet
importlib.metadata.distribution("package_name")
orimportlib.metadata.distributions(path=["..."], name="package_name")
[d.metadata["..."] for d in importlib.metadata.distributions()]
-
importlib.metadata.Distribution.at(info_dir).entry_points
next(importlib.metadata.distributions(path=["..."])).read_text("WHEEL")
packaging.utils.canonicalize_name
, etc
As a vendored library, importlib-metadata
would have the following characteristics:
- Apache-2.0 license
- Several backports for Python 2:
contextlib2
, which we already vendorpathlib2
- we have looked at for tests. There is one issue we need to investigate the impact of: Unusable on Windows/Python2.7 with unicode paths jazzband/pathlib2#56 - MIT licensed, pure Python with no dependenciesconfigparser
-configparser
backport - MIT licensed, pure Python with no dependencies
- One dependency otherwise:
zipp
for making thezipfile
modulePath
-aware - MIT licensed, pure Python with no dependencies
Alternative Solutions
- Continue using
pkg_resources
- this approach has the downsides mentioned above - Use another metadata-providing library - none researched, I don't imagine we'd choose something else over either of these two
See also