22
22
from pip ._vendor .packaging .requirements import Requirement as PackagingRequirement
23
23
from pip ._vendor .packaging .specifiers import SpecifierSet
24
24
from pip ._vendor .packaging .utils import NormalizedName , canonicalize_name
25
- from pip ._vendor .pkg_resources import Distribution
26
25
from pip ._vendor .resolvelib import ResolutionImpossible
27
26
28
27
from pip ._internal .cache import CacheEntry , WheelCache
35
34
UnsupportedWheel ,
36
35
)
37
36
from pip ._internal .index .package_finder import PackageFinder
37
+ from pip ._internal .metadata import BaseDistribution , get_default_environment
38
38
from pip ._internal .models .link import Link
39
39
from pip ._internal .models .wheel import Wheel
40
40
from pip ._internal .operations .prepare import RequirementPreparer
46
46
from pip ._internal .resolution .base import InstallRequirementProvider
47
47
from pip ._internal .utils .compatibility_tags import get_supported
48
48
from pip ._internal .utils .hashes import Hashes
49
- from pip ._internal .utils .misc import (
50
- dist_in_site_packages ,
51
- dist_in_usersite ,
52
- get_installed_distributions ,
53
- )
54
49
from pip ._internal .utils .virtualenv import running_under_virtualenv
55
50
56
51
from .base import Candidate , CandidateVersion , Constraint , Requirement
@@ -122,9 +117,10 @@ def __init__(
122
117
] = {}
123
118
124
119
if not ignore_installed :
120
+ env = get_default_environment ()
125
121
self ._installed_dists = {
126
- canonicalize_name ( dist .project_name ) : dist
127
- for dist in get_installed_distributions (local_only = False )
122
+ dist .canonical_name : dist
123
+ for dist in env . iter_installed_distributions (local_only = False )
128
124
}
129
125
else :
130
126
self ._installed_dists = {}
@@ -155,15 +151,15 @@ def _make_extras_candidate(
155
151
156
152
def _make_candidate_from_dist (
157
153
self ,
158
- dist : Distribution ,
154
+ dist : BaseDistribution ,
159
155
extras : FrozenSet [str ],
160
156
template : InstallRequirement ,
161
157
) -> Candidate :
162
158
try :
163
- base = self ._installed_candidate_cache [dist .key ]
159
+ base = self ._installed_candidate_cache [dist .canonical_name ]
164
160
except KeyError :
165
161
base = AlreadyInstalledCandidate (dist , template , factory = self )
166
- self ._installed_candidate_cache [dist .key ] = base
162
+ self ._installed_candidate_cache [dist .canonical_name ] = base
167
163
if not extras :
168
164
return base
169
165
return self ._make_extras_candidate (base , extras )
@@ -520,7 +516,7 @@ def get_wheel_cache_entry(
520
516
supported_tags = get_supported (),
521
517
)
522
518
523
- def get_dist_to_uninstall (self , candidate : Candidate ) -> Optional [Distribution ]:
519
+ def get_dist_to_uninstall (self , candidate : Candidate ) -> Optional [BaseDistribution ]:
524
520
# TODO: Are there more cases this needs to return True? Editable?
525
521
dist = self ._installed_dists .get (candidate .project_name )
526
522
if dist is None : # Not installed, no uninstallation required.
@@ -533,21 +529,19 @@ def get_dist_to_uninstall(self, candidate: Candidate) -> Optional[Distribution]:
533
529
return dist
534
530
535
531
# We're installing into user site. Remove the user site installation.
536
- if dist_in_usersite ( dist ) :
532
+ if dist . in_usersite :
537
533
return dist
538
534
539
535
# We're installing into user site, but the installed incompatible
540
536
# package is in global site. We can't uninstall that, and would let
541
537
# the new user installation to "shadow" it. But shadowing won't work
542
538
# in virtual environments, so we error out.
543
- if running_under_virtualenv () and dist_in_site_packages (dist ):
544
- raise InstallationError (
545
- "Will not install to the user site because it will "
546
- "lack sys.path precedence to {} in {}" .format (
547
- dist .project_name ,
548
- dist .location ,
549
- )
539
+ if running_under_virtualenv () and dist .in_site_packages :
540
+ message = (
541
+ f"Will not install to the user site because it will lack "
542
+ f"sys.path precedence to { dist .raw_name } in { dist .location } "
550
543
)
544
+ raise InstallationError (message )
551
545
return None
552
546
553
547
def _report_requires_python_error (
0 commit comments