Skip to content

gh-72327: Suggest using system terminal for pip install in PyREPL #136328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 15, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
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 <tom@viner.tv>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
  • Loading branch information
3 people committed Jul 5, 2025
commit 86039e66481ccba9957fa3779e5f0a03e6b5b23d
15 changes: 14 additions & 1 deletion Lib/_pyrepl/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import linecache
from dataclasses import dataclass, field
import os.path
import re
import sys


Expand Down Expand Up @@ -195,7 +196,19 @@ def runsource(self, source, filename="<input>", 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:
Expand Down
11 changes: 11 additions & 0 deletions Lib/test/test_pyrepl/test_pyrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 2 additions & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1742,6 +1742,7 @@ Joel Shprentz
Yue Shuaijie
Jaysinh Shukla
Terrel Shumway
Richard Si
Eric Siegerman
Reilly Tucker Siemens
Paul Sijben
Expand Down Expand Up @@ -1986,6 +1987,7 @@ Olivier Vielpeau
Kannan Vijayan
Kurt Vile
Norman Vine
Tom Viner
Pauli Virtanen
Frank Visser
Long Vo
Expand Down
Original file line number Diff line number Diff line change
@@ -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.