@@ -25,20 +25,30 @@ def _should_use_importlib_metadata() -> bool:
2525 """Whether to use the ``importlib.metadata`` or ``pkg_resources`` backend.
2626
2727 By default, pip uses ``importlib.metadata`` on Python 3.11+, and
28- ``pkg_resources`` otherwise. This can be overridden by a couple of ways:
28+ ``pkg_resources`` otherwise. Up to Python 3.13, This can be
29+ overridden by a couple of ways:
2930
3031 * If environment variable ``_PIP_USE_IMPORTLIB_METADATA`` is set, it
31- dictates whether ``importlib.metadata`` is used, regardless of Python
32- version.
33- * On Python 3.11+, Python distributors can patch ``importlib.metadata``
34- to add a global constant ``_PIP_USE_IMPORTLIB_METADATA = False``. This
35- makes pip use ``pkg_resources`` (unless the user set the aforementioned
36- environment variable to *True*).
32+ dictates whether ``importlib.metadata`` is used, for Python <3.14.
33+ * On Python 3.11, 3.12 and 3.13, Python distributors can patch
34+ ``importlib.metadata`` to add a global constant
35+ ``_PIP_USE_IMPORTLIB_METADATA = False``. This makes pip use
36+ ``pkg_resources`` (unless the user set the aforementioned environment
37+ variable to *True*).
38+
39+ On Python 3.14+, the ``pkg_resources`` backend cannot be used.
3740 """
41+ if sys .version_info >= (3 , 14 ):
42+ # On Python >=3.14 we only support importlib.metadata.
43+ return True
3844 with contextlib .suppress (KeyError , ValueError ):
45+ # On Python <3.14, if the environment variable is set, we obey what it says.
3946 return bool (strtobool (os .environ ["_PIP_USE_IMPORTLIB_METADATA" ]))
4047 if sys .version_info < (3 , 11 ):
48+ # On Python <3.11, we always use pkg_resources, unless the environment
49+ # variable was set.
4150 return False
51+ # On Python 3.11, 3.12 and 3.13, we check if the global constant is set.
4252 import importlib .metadata
4353
4454 return bool (getattr (importlib .metadata , "_PIP_USE_IMPORTLIB_METADATA" , True ))
0 commit comments