From 14bd7fa1e18b8d545e86fc50e46b9598f024cf0a Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Wed, 10 Jun 2026 18:29:07 +0330 Subject: [PATCH 1/4] Allow stderr to be shown instead of being merged --- Lib/venv/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index bd2762d55ef6961..a8ad1590bab926c 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -460,7 +460,7 @@ def _call_new_python(self, context, *py_args, **kwargs): def _setup_pip(self, context): """Installs or upgrades pip in a virtual environment""" self._call_new_python(context, '-m', 'ensurepip', '--upgrade', - '--default-pip', stderr=subprocess.STDOUT) + '--default-pip') def setup_scripts(self, context): """ From eac149f704a681aa6a859e3ff0c2278ddec9004e Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Wed, 10 Jun 2026 18:51:25 +0330 Subject: [PATCH 2/4] Add tests --- Lib/test/test_venv.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 1ff5d7cf0c51dd8..69f6ed17eb022bc 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -963,6 +963,23 @@ def test_devnull(self): self.assertTrue(os.path.exists(os.devnull)) + def test_ensurepip_failure_shows_stderr(self): + """Test that ensurepip failure output is shown to the user (gh-89830)""" + with tempfile.TemporaryDirectory() as env_dir: + builder = venv.EnvBuilder(with_pip=True) + # Simulate ensurepip failing with a traceback on stderr + def fake_call_new_python(context, *args, **kwargs): + raise subprocess.CalledProcessError( + returncode=1, + cmd=args, + stderr=b"AttributeError: simulated ensurepip failure" + ) + with patch.object(venv.EnvBuilder, '_call_new_python', fake_call_new_python): + with captured_stderr() as err: + with self.assertRaises(subprocess.CalledProcessError): + builder.create(env_dir) + self.assertIn("simulated ensurepip failure", err.getvalue()) + def do_test_with_pip(self, system_site_packages): rmtree(self.env_dir) with EnvironmentVarGuard() as envvars: From da61081ad0c00244a4807f66f802b520708df17d Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Wed, 10 Jun 2026 19:01:19 +0330 Subject: [PATCH 3/4] Add news file --- .../next/Library/2026-06-10-19-00-53.gh-issue-89830.YGHExb.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-06-10-19-00-53.gh-issue-89830.YGHExb.rst diff --git a/Misc/NEWS.d/next/Library/2026-06-10-19-00-53.gh-issue-89830.YGHExb.rst b/Misc/NEWS.d/next/Library/2026-06-10-19-00-53.gh-issue-89830.YGHExb.rst new file mode 100644 index 000000000000000..3e2ece090db8ca0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-10-19-00-53.gh-issue-89830.YGHExb.rst @@ -0,0 +1,2 @@ +Fix unhelpful error message when :mod:`venv` fails to install pip via +:mod:`ensurepip` by displaying the subprocess stderr output. From 8a592287ae21d19e5c0392e93f87e96034dabe7a Mon Sep 17 00:00:00 2001 From: sepehrrasooli Date: Wed, 10 Jun 2026 19:39:43 +0330 Subject: [PATCH 4/4] Catch errors to fix failing test --- Lib/venv/__init__.py | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Lib/venv/__init__.py b/Lib/venv/__init__.py index a8ad1590bab926c..e2ec7a9ae1d6cc6 100644 --- a/Lib/venv/__init__.py +++ b/Lib/venv/__init__.py @@ -459,8 +459,13 @@ def _call_new_python(self, context, *py_args, **kwargs): def _setup_pip(self, context): """Installs or upgrades pip in a virtual environment""" - self._call_new_python(context, '-m', 'ensurepip', '--upgrade', - '--default-pip') + try: + self._call_new_python(context, '-m', 'ensurepip', '--upgrade', + '--default-pip', stderr=subprocess.PIPE) + except subprocess.CalledProcessError as e: + if e.stderr: + print(e.stderr.decode(errors='replace'), file=sys.stderr) + raise def setup_scripts(self, context): """