diff --git a/Lib/ensurepip/__init__.py b/Lib/ensurepip/__init__.py index 3fbe8b2a5b1367..a4c46ec65fe8db 100644 --- a/Lib/ensurepip/__init__.py +++ b/Lib/ensurepip/__init__.py @@ -90,8 +90,16 @@ def _run_pip(args, additional_paths=None): sys.argv[1:] = {args} runpy.run_module("pip", run_name="__main__", alter_sys=True) """ - return subprocess.run([sys.executable, '-W', 'ignore::DeprecationWarning', - "-c", code], check=True).returncode + + cmd = [ + sys.executable, + # run code in isolated mode if currently running isolated + *([ '-I'] if sys.flags.isolated else []), + '-W', 'ignore::DeprecationWarning', + '-c', + code ] + + return subprocess.run( cmd, check = True ).returncode def version(): diff --git a/Misc/NEWS.d/next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst b/Misc/NEWS.d/next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst new file mode 100644 index 00000000000000..3a4f5e0edf7bf6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-01-03-15-07-06.bpo-46197.Z0djv6.rst @@ -0,0 +1 @@ +Fix :mod:`ensurepip` environment isolation for sub-process. \ No newline at end of file