import linecache
from dataclasses import dataclass, field
import os.path
+import re
import sys
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*(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"
+ "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:
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 sampleproject", "exit"])
+ self.assertIn("SyntaxError", output)
+ hint = (
+ "The Python package manager (pip) can only be used"
+ " outside of the Python REPL"
+ )
+ self.assertIn(hint, output)
Yue Shuaijie
Jaysinh Shukla
Terrel Shumway
+Richard Si
Eric Siegerman
Reilly Tucker Siemens
Paul Sijben
Kannan Vijayan
Kurt Vile
Norman Vine
+Tom Viner
Pauli Virtanen
Frank Visser
Long Vo
--- /dev/null
+Suggest using the system command prompt when ``pip install`` is typed into
+the REPL. Patch by Tom Viner, Richard Si, and Brian Schubert.