@@ -308,14 +308,25 @@ def setup_python(self, context):
308308 shutil .copyfile (src , dst )
309309 break
310310
311+ def _call_new_python (self , context , * py_args , ** kwargs ):
312+ """Executes the newly created Python using safe-ish options"""
313+ # gh-98251: We do not want to just use '-I' because that masks
314+ # legitimate user preferences (such as not writing bytecode). All we
315+ # really need is to ensure that the path variables do not overrule
316+ # normal venv handling.
317+ args = [context .env_exec_cmd , * py_args ]
318+ kwargs ['env' ] = env = os .environ .copy ()
319+ env ['VIRTUAL_ENV' ] = context .env_dir
320+ env .pop ('PYTHONHOME' , None )
321+ env .pop ('PYTHONPATH' , None )
322+ kwargs ['cwd' ] = context .env_dir
323+ kwargs ['executable' ] = context .env_exec_cmd
324+ subprocess .check_output (args , ** kwargs )
325+
311326 def _setup_pip (self , context ):
312327 """Installs or upgrades pip in a virtual environment"""
313- # We run ensurepip in isolated mode to avoid side effects from
314- # environment vars, the current directory and anything else
315- # intended for the global Python environment
316- cmd = [context .env_exec_cmd , '-Im' , 'ensurepip' , '--upgrade' ,
317- '--default-pip' ]
318- subprocess .check_output (cmd , stderr = subprocess .STDOUT )
328+ self ._call_new_python (context , '-m' , 'ensurepip' , '--upgrade' ,
329+ '--default-pip' , stderr = subprocess .STDOUT )
319330
320331 def setup_scripts (self , context ):
321332 """
@@ -414,9 +425,8 @@ def upgrade_dependencies(self, context):
414425 logger .debug (
415426 f'Upgrading { CORE_VENV_DEPS } packages in { context .bin_path } '
416427 )
417- cmd = [context .env_exec_cmd , '-m' , 'pip' , 'install' , '--upgrade' ]
418- cmd .extend (CORE_VENV_DEPS )
419- subprocess .check_call (cmd )
428+ self ._call_new_python (context , '-m' , 'pip' , 'install' , '--upgrade' ,
429+ * CORE_VENV_DEPS )
420430
421431
422432def create (env_dir , system_site_packages = False , clear = False ,
0 commit comments