Skip to content

Commit 450fd26

Browse files
committed
Import distutils only if needed, but sooner
1 parent 0c9eb96 commit 450fd26

File tree

2 files changed

+7
-8
lines changed

2 files changed

+7
-8
lines changed

news/11319.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix import error when reinstalling pip in user site.

src/pip/_internal/locations/__init__.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ def _should_use_sysconfig() -> bool:
6060

6161
_USE_SYSCONFIG = _should_use_sysconfig()
6262

63+
if not _USE_SYSCONFIG:
64+
# Import distutils lazily to avoid deprecation warnings,
65+
# but import it soon enough that it is in memory and available during
66+
# a pip reinstall.
67+
from . import _distutils
68+
6369
# Be noisy about incompatibilities if this platforms "should" be using
6470
# sysconfig, but is explicitly opting out and using distutils instead.
6571
if _USE_SYSCONFIG_DEFAULT and not _USE_SYSCONFIG:
@@ -241,8 +247,6 @@ def get_scheme(
241247
if _USE_SYSCONFIG:
242248
return new
243249

244-
from . import _distutils
245-
246250
old = _distutils.get_scheme(
247251
dist_name,
248252
user=user,
@@ -407,8 +411,6 @@ def get_bin_prefix() -> str:
407411
if _USE_SYSCONFIG:
408412
return new
409413

410-
from . import _distutils
411-
412414
old = _distutils.get_bin_prefix()
413415
if _warn_if_mismatch(pathlib.Path(old), pathlib.Path(new), key="bin_prefix"):
414416
_log_context()
@@ -442,8 +444,6 @@ def get_purelib() -> str:
442444
if _USE_SYSCONFIG:
443445
return new
444446

445-
from . import _distutils
446-
447447
old = _distutils.get_purelib()
448448
if _looks_like_deb_system_dist_packages(old):
449449
return old
@@ -488,8 +488,6 @@ def get_prefixed_libs(prefix: str) -> List[str]:
488488
if _USE_SYSCONFIG:
489489
return _deduplicated(new_pure, new_plat)
490490

491-
from . import _distutils
492-
493491
old_pure, old_plat = _distutils.get_prefixed_libs(prefix)
494492
old_lib_paths = _deduplicated(old_pure, old_plat)
495493

0 commit comments

Comments
 (0)