Skip to content
Closed
Show file tree
Hide file tree
Changes from 5 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
22 changes: 22 additions & 0 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,23 @@ def setcopyright():
files, dirs)


def _register_detect_pip_usage_in_repl():
old_excepthook = sys.excepthook

def detect_pip_usage_in_repl(typ, value, traceback):
if typ is SyntaxError and (
"pip install" in value.text or "pip3 install" in value.text
):
value.msg = ("The Python package manager (pip) can only be used"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since there is a chance of false detection, it is worth to include the original message in the result.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original message is just "invalid syntax", so to put that back in would require going from what we have now:

  File "<string>", line 1
    pip install qwerty
              ^
SyntaxError: The Python package manager (pip) can only be used from outside of Python.
Please try the `pip` command in a separate terminal or command prompt.

to something like the more expansive:

  File "<string>", line 1
    pip install qwerty
              ^
SyntaxError: invalid syntax

Note: The Python package manager (pip) can only be used from outside of Python.
Please try the `pip` command in a separate terminal or command prompt.

I'm happy with either.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not always "invalid syntax".

>>>     x = 'pip install'
  File "<stdin>", line 1
    x = 'pip install'
    ^
IndentationError: unexpected indent

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is why the original suggestion from ncoghlan has is SyntaxError rather than isinstance, so subclasses (IndentationError & TabError) don't get caught.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not always "invalid syntax" even if the type is SyntaxError.

>>> x = ['pip install')
  File "<stdin>", line 1
    x = ['pip install')
                      ^
SyntaxError: closing parenthesis ')' does not match opening parenthesis '['

Also I think that we perhaps can use __notes__ here.

" from outside of Python.\n"
"Please try the `pip` command in a"
" separate terminal or command prompt.")

old_excepthook(typ, value, traceback)

sys.excepthook = detect_pip_usage_in_repl


def sethelper():
builtins.help = _sitebuiltins._Helper()

Expand Down Expand Up @@ -558,6 +575,11 @@ def main():
setquit()
setcopyright()
sethelper()
# Detect REPL by prompt, -i flag or PYTHONINSPECT env var being set.
if (hasattr(sys, 'ps1') or sys.flags.interactive or
os.environ.get('PYTHONINSPECT')
):
_register_detect_pip_usage_in_repl()
if not sys.flags.isolated:
enablerlcompleter()
execsitecustomize()
Expand Down
23 changes: 23 additions & 0 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import test.support
from test.support import captured_stderr, TESTFN, EnvironmentVarGuard
import builtins
import io
import os
import sys
import re
Expand Down Expand Up @@ -454,6 +455,28 @@ def test_license_exists_at_url(self):
self.assertEqual(code, 200, msg="Can't find " + url)


class DetectPipUsageInReplTests(unittest.TestCase):
def setUp(self):
self.old_excepthook = sys.excepthook
site._register_detect_pip_usage_in_repl()

def tearDown(self):
sys.excepthook = self.old_excepthook

def test_detect_pip_usage_in_repl(self):
for pip_cmd in [
'pip install a', 'pip3 install b', 'python -m pip install c'
]:
with self.subTest(pip_cmd=pip_cmd):
try:
exec(pip_cmd, {}, {})
except SyntaxError as exc:
with captured_stderr() as err_out:
sys.excepthook(SyntaxError, exc, exc.__traceback__)

self.assertIn("the `pip` command", err_out.getvalue())


class StartupImportTests(unittest.TestCase):

def test_startup_imports(self):
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1691,6 +1691,7 @@ Olivier Vielpeau
Kannan Vijayan
Kurt Vile
Norman Vine
Tom Viner
Pauli Virtanen
Frank Visser
Johannes Vogel
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Give better errors for ``pip install`` command typed into the REPL.