Skip to content

Commit be02e68

Browse files
ichard26tomvinerbrianschuberthugovkncoghlan
authored
gh-72327: Suggest using system terminal for pip install in PyREPL (#136328)
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. Closes #72327 --------- Co-authored-by: Tom Viner <[email protected]> Co-authored-by: Brian Schubert <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: Alyssa Coghlan <[email protected]>
1 parent a8f42e6 commit be02e68

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

Lib/_pyrepl/console.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import linecache
2828
from dataclasses import dataclass, field
2929
import os.path
30+
import re
3031
import sys
3132

3233

@@ -195,7 +196,19 @@ def runsource(self, source, filename="<input>", symbol="single"):
195196
ast.PyCF_ONLY_AST,
196197
incomplete_input=False,
197198
)
198-
except (SyntaxError, OverflowError, ValueError):
199+
except SyntaxError as e:
200+
# If it looks like pip install was entered (a common beginner
201+
# mistake), provide a hint to use the system command prompt.
202+
if re.match(r"^\s*(pip3?|py(thon3?)? -m pip) install.*", source):
203+
e.add_note(
204+
"The Python package manager (pip) can only be used"
205+
" outside of the Python REPL.\n"
206+
"Try the 'pip' command in a separate terminal or"
207+
" command prompt."
208+
)
209+
self.showsyntaxerror(filename, source=source)
210+
return False
211+
except (OverflowError, ValueError):
199212
self.showsyntaxerror(filename, source=source)
200213
return False
201214
if tree.body:

Lib/test/test_pyrepl/test_pyrepl.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,3 +1757,14 @@ def test_showrefcount(self):
17571757
output, _ = self.run_repl("1\n1+2\nexit()\n", cmdline_args=['-Xshowrefcount'], env=env)
17581758
matches = re.findall(r'\[-?\d+ refs, \d+ blocks\]', output)
17591759
self.assertEqual(len(matches), 3)
1760+
1761+
def test_detect_pip_usage_in_repl(self):
1762+
for pip_cmd in ("pip", "pip3", "python -m pip", "python3 -m pip"):
1763+
with self.subTest(pip_cmd=pip_cmd):
1764+
output, exit_code = self.run_repl([f"{pip_cmd} install sampleproject", "exit"])
1765+
self.assertIn("SyntaxError", output)
1766+
hint = (
1767+
"The Python package manager (pip) can only be used"
1768+
" outside of the Python REPL"
1769+
)
1770+
self.assertIn(hint, output)

Misc/ACKS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1744,6 +1744,7 @@ Joel Shprentz
17441744
Yue Shuaijie
17451745
Jaysinh Shukla
17461746
Terrel Shumway
1747+
Richard Si
17471748
Eric Siegerman
17481749
Reilly Tucker Siemens
17491750
Paul Sijben
@@ -1988,6 +1989,7 @@ Olivier Vielpeau
19881989
Kannan Vijayan
19891990
Kurt Vile
19901991
Norman Vine
1992+
Tom Viner
19911993
Pauli Virtanen
19921994
Frank Visser
19931995
Long Vo
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Suggest using the system command prompt when ``pip install`` is typed into
2+
the REPL. Patch by Tom Viner, Richard Si, and Brian Schubert.

0 commit comments

Comments
 (0)