Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
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.
Loading