Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prevent pkg_resource access on Python 3.12+ #11661

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions news/11501.process.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Prevent the ``pkg_resources`` metadata backend from being used on Python 3.12
or later. This backend exists for backward compatibility, and should only be
used on newer Python versions by Python distributors that cannot correctly
implement ``importlib.metadata`` support in a timely fashion. Time is up for
transition since CPython is now removing mechanisms that ``pkg_resource``
relies on, and the backend will no longer work on Python 3.12 or later.
5 changes: 4 additions & 1 deletion src/pip/_internal/metadata/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ def _should_use_importlib_metadata() -> bool:
"""Whether to use the ``importlib.metadata`` or ``pkg_resources`` backend.

By default, pip uses ``importlib.metadata`` on Python 3.11+, and
``pkg_resourcess`` otherwise. This can be overridden by a couple of ways:
``pkg_resources`` otherwise. On Python versions prior to 3.12, this can be
overridden by a couple of ways for transitional purposes:

* If environment variable ``_PIP_USE_IMPORTLIB_METADATA`` is set, it
dictates whether ``importlib.metadata`` is used, regardless of Python
Expand All @@ -40,6 +41,8 @@ def _should_use_importlib_metadata() -> bool:
makes pip use ``pkg_resources`` (unless the user set the aforementioned
environment variable to *True*).
"""
if sys.version_info >= (3, 12):
return True
with contextlib.suppress(KeyError, ValueError):
return bool(strtobool(os.environ["_PIP_USE_IMPORTLIB_METADATA"]))
if sys.version_info < (3, 11):
Expand Down