From db4751595867db1d938df7183a60dfb15fa0d708 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Bidoul?= Date: Fri, 29 Jul 2022 22:35:47 +0200 Subject: [PATCH] Import distutils only if needed, but sooner --- news/11319.bugfix.rst | 1 + src/pip/_internal/locations/__init__.py | 14 ++++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) create mode 100644 news/11319.bugfix.rst diff --git a/news/11319.bugfix.rst b/news/11319.bugfix.rst new file mode 100644 index 00000000000..31cd2a34b0b --- /dev/null +++ b/news/11319.bugfix.rst @@ -0,0 +1 @@ +Fix import error when reinstalling pip in user site. diff --git a/src/pip/_internal/locations/__init__.py b/src/pip/_internal/locations/__init__.py index 3f6f0a58e16..60afe0a73b8 100644 --- a/src/pip/_internal/locations/__init__.py +++ b/src/pip/_internal/locations/__init__.py @@ -60,6 +60,12 @@ def _should_use_sysconfig() -> bool: _USE_SYSCONFIG = _should_use_sysconfig() +if not _USE_SYSCONFIG: + # Import distutils lazily to avoid deprecation warnings, + # but import it soon enough that it is in memory and available during + # a pip reinstall. + from . import _distutils + # Be noisy about incompatibilities if this platforms "should" be using # sysconfig, but is explicitly opting out and using distutils instead. if _USE_SYSCONFIG_DEFAULT and not _USE_SYSCONFIG: @@ -241,8 +247,6 @@ def get_scheme( if _USE_SYSCONFIG: return new - from . import _distutils - old = _distutils.get_scheme( dist_name, user=user, @@ -407,8 +411,6 @@ def get_bin_prefix() -> str: if _USE_SYSCONFIG: return new - from . import _distutils - old = _distutils.get_bin_prefix() if _warn_if_mismatch(pathlib.Path(old), pathlib.Path(new), key="bin_prefix"): _log_context() @@ -442,8 +444,6 @@ def get_purelib() -> str: if _USE_SYSCONFIG: return new - from . import _distutils - old = _distutils.get_purelib() if _looks_like_deb_system_dist_packages(old): return old @@ -488,8 +488,6 @@ def get_prefixed_libs(prefix: str) -> List[str]: if _USE_SYSCONFIG: return _deduplicated(new_pure, new_plat) - from . import _distutils - old_pure, old_plat = _distutils.get_prefixed_libs(prefix) old_lib_paths = _deduplicated(old_pure, old_plat)