@@ -73,17 +73,20 @@ def _minimum_pip_version() -> str:
73
73
def _has_valid_pip (** distargs : object ) -> bool :
74
74
"""
75
75
Given a path, see if Pip is present and return True if the version is
76
- sufficient for build, False if it is not.
76
+ sufficient for build, False if it is not. ModuleNotFoundError is thrown if
77
+ pip is not present.
77
78
"""
78
79
79
80
import packaging .version
80
81
81
- if sys .version_info < (3 , 8 ):
82
- import importlib_metadata as metadata
83
- else :
84
- from importlib import metadata
82
+ from ._importlib import metadata
83
+
84
+ name = 'pip'
85
85
86
- pip_distribution = next (iter (metadata .distributions (name = 'pip' , ** distargs )))
86
+ try :
87
+ pip_distribution = next (iter (metadata .distributions (name = name , ** distargs )))
88
+ except StopIteration :
89
+ raise ModuleNotFoundError (name ) from None
87
90
88
91
current_pip_version = packaging .version .Version (pip_distribution .version )
89
92
@@ -93,16 +96,13 @@ def _has_valid_pip(**distargs: object) -> bool:
93
96
@functools .lru_cache (maxsize = None )
94
97
def _valid_global_pip () -> bool | None :
95
98
"""
96
- This checks for a valid global pip. Returns None if the prerequisites are
97
- not available (Python 3.7 only) or pip is missing, False if Pip is too old,
98
- and True if it can be used.
99
+ This checks for a valid global pip. Returns None if pip is missing, False
100
+ if Pip is too old, and True if it can be used.
99
101
"""
100
102
101
103
try :
102
104
return _has_valid_pip ()
103
- except ModuleNotFoundError : # Python 3.7 only
104
- return None
105
- except StopIteration :
105
+ except ModuleNotFoundError :
106
106
return None
107
107
108
108
@@ -155,11 +155,11 @@ def python_executable(self) -> str:
155
155
"""The python executable of the isolated build environment."""
156
156
return self ._python_executable
157
157
158
- def _pip_args (self , * , isolate : bool = False ) -> list [str ]:
158
+ def _pip_args (self ) -> list [str ]:
159
159
if _valid_global_pip ():
160
- return [sys .executable , '-Im' if isolate else '-m' , 'pip' , '--python' , self .python_executable ]
160
+ return [sys .executable , '-Im' , 'pip' , '--python' , self .python_executable ]
161
161
else :
162
- return [self .python_executable , '-Im' if isolate else '-m' , 'pip' ]
162
+ return [self .python_executable , '-Im' , 'pip' ]
163
163
164
164
def make_extra_environ (self ) -> dict [str , str ]:
165
165
path = os .environ .get ('PATH' )
@@ -185,7 +185,7 @@ def install(self, requirements: Collection[str]) -> None:
185
185
req_file .write (os .linesep .join (requirements ))
186
186
try :
187
187
cmd = [
188
- * self ._pip_args (isolate = True ),
188
+ * self ._pip_args (),
189
189
'install' ,
190
190
'--use-pep517' ,
191
191
'--no-warn-script-location' ,
@@ -222,9 +222,9 @@ def _create_isolated_env_virtualenv(path: str) -> tuple[str, str]:
222
222
import virtualenv
223
223
224
224
if _valid_global_pip ():
225
- cmd = [str ( path ) , '--no-seed' , '--activators' , '' ]
225
+ cmd = [path , '--no-seed' , '--activators' , '' ]
226
226
else :
227
- cmd = [str ( path ) , '--no-setuptools' , '--no-wheel' , '--activators' , '' ]
227
+ cmd = [path , '--no-setuptools' , '--no-wheel' , '--activators' , '' ]
228
228
229
229
result = virtualenv .cli_run (cmd , setup_logging = False )
230
230
executable = str (result .creator .exe )
@@ -275,9 +275,7 @@ def _create_isolated_env_venv(path: str) -> tuple[str, str]:
275
275
_subprocess ([executable , '-m' , 'pip' , 'install' , f'pip>={ _minimum_pip_version ()} ' ])
276
276
277
277
# Avoid the setuptools from ensurepip to break the isolation
278
- if _valid_global_pip ():
279
- _subprocess ([sys .executable , '-m' , 'pip' , '--python' , executable , 'uninstall' , 'setuptools' , '-y' ])
280
- else :
278
+ if not _valid_global_pip ():
281
279
_subprocess ([executable , '-m' , 'pip' , 'uninstall' , 'setuptools' , '-y' ])
282
280
283
281
return executable , script_dir
0 commit comments