From 86039e66481ccba9957fa3779e5f0a03e6b5b23d Mon Sep 17 00:00:00 2001 From: Richard Si Date: Sat, 5 Jul 2025 18:46:17 -0400 Subject: [PATCH 1/3] gh-72327: Suggest using system terminal for pip install in REPL Users new to Python packaging often try to use pip from the REPL only to be met with a confusing SyntaxError. If this happens, guide the user to use a system terminal instead to invoke pip. Co-authored-by: Tom Viner Co-authored-by: Brian Schubert --- Lib/_pyrepl/console.py | 15 ++++++++++++++- Lib/test/test_pyrepl/test_pyrepl.py | 11 +++++++++++ Misc/ACKS | 2 ++ .../2025-07-07-16-46-55.gh-issue-72327.wLvRuj.rst | 2 ++ 4 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-07-07-16-46-55.gh-issue-72327.wLvRuj.rst diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index 8956fb1242e52a..85f0a7cfd33f15 100644 --- a/Lib/_pyrepl/console.py +++ b/Lib/_pyrepl/console.py @@ -27,6 +27,7 @@ import linecache from dataclasses import dataclass, field import os.path +import re import sys @@ -195,7 +196,19 @@ def runsource(self, source, filename="", symbol="single"): ast.PyCF_ONLY_AST, incomplete_input=False, ) - except (SyntaxError, OverflowError, ValueError): + except SyntaxError as e: + # If it looks like pip install was entered (a common beginner + # mistake), provide a hint to use the system command prompt. + if re.match(r"^\s*(py(thon3?)? -m pip|pip3?) install.*", source): + e.add_note( + "The Python package manager (pip) can only be used" + " outside of the Python REPL.\n" + "Try the 'pip' command in a separate terminal or" + " command prompt." + ) + self.showsyntaxerror(filename, source=source) + return False + except (OverflowError, ValueError): self.showsyntaxerror(filename, source=source) return False if tree.body: diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index 98bae7dd703fd9..e7bf39b3995bb3 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1757,3 +1757,14 @@ def test_showrefcount(self): output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env) matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output) self.assertEqual(len(matches), 3) + + def test_detect_pip_usage_in_repl(self): + for pip_cmd in ("pip", "pip3", "python -m pip", "python3 -m pip"): + with self.subTest(pip_cmd=pip_cmd): + output, exit_code = self.run_repl([f"{pip_cmd} install antigravity", "exit"]) + self.assertIn("SyntaxError", output) + hint = ( + "The Python package manager (pip) can only be used" + " outside of the Python REPL" + ) + self.assertIn(hint, output) diff --git a/Misc/ACKS b/Misc/ACKS index d1490e1e46ccfd..aa0bbdec258d47 100644 --- a/Misc/ACKS +++ b/Misc/ACKS @@ -1742,6 +1742,7 @@ Joel Shprentz Yue Shuaijie Jaysinh Shukla Terrel Shumway +Richard Si Eric Siegerman Reilly Tucker Siemens Paul Sijben @@ -1986,6 +1987,7 @@ Olivier Vielpeau Kannan Vijayan Kurt Vile Norman Vine +Tom Viner Pauli Virtanen Frank Visser Long Vo diff --git a/Misc/NEWS.d/next/Library/2025-07-07-16-46-55.gh-issue-72327.wLvRuj.rst b/Misc/NEWS.d/next/Library/2025-07-07-16-46-55.gh-issue-72327.wLvRuj.rst new file mode 100644 index 00000000000000..f305abb655a6f6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-07-07-16-46-55.gh-issue-72327.wLvRuj.rst @@ -0,0 +1,2 @@ +Suggest using the system command prompt when ``pip install`` is typed into +the REPL. Patch by Tom Viner, Richard Si, and Brian Schubert. From 187542af0a215f46a200550932e5d0adbd8af670 Mon Sep 17 00:00:00 2001 From: Richard Si Date: Fri, 11 Jul 2025 10:17:48 -0400 Subject: [PATCH 2/3] Update Lib/test/test_pyrepl/test_pyrepl.py Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com> --- Lib/test/test_pyrepl/test_pyrepl.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_pyrepl/test_pyrepl.py b/Lib/test/test_pyrepl/test_pyrepl.py index e7bf39b3995bb3..657a971f8769df 100644 --- a/Lib/test/test_pyrepl/test_pyrepl.py +++ b/Lib/test/test_pyrepl/test_pyrepl.py @@ -1761,7 +1761,7 @@ def test_showrefcount(self): def test_detect_pip_usage_in_repl(self): for pip_cmd in ("pip", "pip3", "python -m pip", "python3 -m pip"): with self.subTest(pip_cmd=pip_cmd): - output, exit_code = self.run_repl([f"{pip_cmd} install antigravity", "exit"]) + output, exit_code = self.run_repl([f"{pip_cmd} install sampleproject", "exit"]) self.assertIn("SyntaxError", output) hint = ( "The Python package manager (pip) can only be used" From e9cf0faf2b4d6b8e6d276622027894392c4e0870 Mon Sep 17 00:00:00 2001 From: Alyssa Coghlan Date: Tue, 15 Jul 2025 23:57:48 +1000 Subject: [PATCH 3/3] Put simpler regex first --- Lib/_pyrepl/console.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py index 85f0a7cfd33f15..e0535d50396316 100644 --- a/Lib/_pyrepl/console.py +++ b/Lib/_pyrepl/console.py @@ -199,7 +199,7 @@ def runsource(self, source, filename="", symbol="single"): except SyntaxError as e: # If it looks like pip install was entered (a common beginner # mistake), provide a hint to use the system command prompt. - if re.match(r"^\s*(py(thon3?)? -m pip|pip3?) install.*", source): + if re.match(r"^\s*(pip3?|py(thon3?)? -m pip) install.*", source): e.add_note( "The Python package manager (pip) can only be used" " outside of the Python REPL.\n"