@@ -339,14 +339,25 @@ def setup_python(self, context):
339339 shutil .copyfile (src , dst )
340340 break
341341
342+ def _call_new_python (self , context , * py_args , ** kwargs ):
343+ """Executes the newly created Python using safe-ish options"""
344+ # gh-98251: We do not want to just use '-I' because that masks
345+ # legitimate user preferences (such as not writing bytecode). All we
346+ # really need is to ensure that the path variables do not overrule
347+ # normal venv handling.
348+ args = [context .env_exec_cmd , * py_args ]
349+ kwargs ['env' ] = env = os .environ .copy ()
350+ env ['VIRTUAL_ENV' ] = context .env_dir
351+ env .pop ('PYTHONHOME' , None )
352+ env .pop ('PYTHONPATH' , None )
353+ kwargs ['cwd' ] = context .env_dir
354+ kwargs ['executable' ] = context .env_exec_cmd
355+ subprocess .check_output (args , ** kwargs )
356+
342357 def _setup_pip (self , context ):
343358 """Installs or upgrades pip in a virtual environment"""
344- # We run ensurepip in isolated mode to avoid side effects from
345- # environment vars, the current directory and anything else
346- # intended for the global Python environment
347- cmd = [context .env_exec_cmd , '-Im' , 'ensurepip' , '--upgrade' ,
348- '--default-pip' ]
349- subprocess .check_output (cmd , stderr = subprocess .STDOUT )
359+ self ._call_new_python (context , '-m' , 'ensurepip' , '--upgrade' ,
360+ '--default-pip' , stderr = subprocess .STDOUT )
350361
351362 def setup_scripts (self , context ):
352363 """
@@ -445,9 +456,8 @@ def upgrade_dependencies(self, context):
445456 logger .debug (
446457 f'Upgrading { CORE_VENV_DEPS } packages in { context .bin_path } '
447458 )
448- cmd = [context .env_exec_cmd , '-m' , 'pip' , 'install' , '--upgrade' ]
449- cmd .extend (CORE_VENV_DEPS )
450- subprocess .check_call (cmd )
459+ self ._call_new_python (context , '-m' , 'pip' , 'install' , '--upgrade' ,
460+ * CORE_VENV_DEPS )
451461
452462
453463def create (env_dir , system_site_packages = False , clear = False ,
0 commit comments