Skip to content

gh-72327: Add help message for pip in REPL #8536

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

Closed
wants to merge 17 commits into from
Closed
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
Prev Previous commit
Next Next commit
Don't print, set exc.msg instead.
  • Loading branch information
tomviner committed Jul 29, 2018
commit 0784d5a4a94950de867c2a83ff1fd542bbe7afab
11 changes: 6 additions & 5 deletions Lib/site.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,14 +394,15 @@ def _register_detect_pip_usage_in_repl():
old_excepthook = sys.excepthook

def detect_pip_usage_in_repl(typ, value, traceback):
old_excepthook(typ, value, traceback)
if typ is SyntaxError and (
"pip install" in value.text or "pip3 install" in value.text
):
print("\nThe Python package manager (pip) can only be used from"
" outside of Python.\nPlease try the `pip` command in a"
" separate terminal or command prompt.",
file=sys.stderr)
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

Expand Down
10 changes: 3 additions & 7 deletions Lib/test/test_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,26 +459,22 @@ class DetectPipUsageInReplTests(unittest.TestCase):
def setUp(self):
self.old_excepthook = sys.excepthook
site._register_detect_pip_usage_in_repl()
self.save_stderr = sys.stderr

def tearDown(self):
sys.stderr = self.save_stderr
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):
stream = io.StringIO()
sys.stderr = stream

try:
exec(pip_cmd, {}, {})
except SyntaxError as exc:
sys.excepthook(SyntaxError, exc, exc.__traceback__)
with captured_stderr() as err_out:
sys.excepthook(SyntaxError, exc, exc.__traceback__)

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


class StartupImportTests(unittest.TestCase):
Expand Down