Skip to content

Commit

Permalink
Import distutils only if needed, but sooner
Browse files Browse the repository at this point in the history
  • Loading branch information
sbidoul committed Jul 30, 2022
1 parent 0c9eb96 commit 450fd26
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
1 change: 1 addition & 0 deletions news/11319.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix import error when reinstalling pip in user site.
14 changes: 6 additions & 8 deletions src/pip/_internal/locations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -241,8 +247,6 @@ def get_scheme(
if _USE_SYSCONFIG:
return new

from . import _distutils

old = _distutils.get_scheme(
dist_name,
user=user,
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 450fd26

Please sign in to comment.