Skip to content

gh-72327: Suggest using system terminal for pip install in PyREPL #136328

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

ichard26
Copy link

@ichard26 ichard26 commented Jul 5, 2025

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. The pip project still receives the occasional issue from users lamenting that "pip is not working!" (pypa/pip#13409)

image

This is an updated version of #8536.

cc @ncoghlan

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.

Co-authored-by: Tom Viner <tom@viner.tv>
Co-authored-by: Brian Schubert <brianm.schubert@gmail.com>
@brianschubert
Copy link
Member

The new REPL has a similar special case for when await is used at the top-level:

except SyntaxError as e:
if e.args[0] == "'await' outside function":
python = os.path.basename(sys.executable)
e.add_note(
f"Try the asyncio REPL ({python} -m asyncio) to use"
f" top-level 'await' and run background asyncio tasks."
)
self.showsyntaxerror(filename, source=source)

Maybe this could be done in a similar way?

Something like this might work:

:...skipping...
diff --git a/Lib/_pyrepl/console.py b/Lib/_pyrepl/console.py
index 8956fb1242e..833e79ef3c2 100644
--- a/Lib/_pyrepl/console.py
+++ b/Lib/_pyrepl/console.py
@@ -195,7 +195,15 @@ def runsource(self, source, filename="<input>", symbol="single"):
                 ast.PyCF_ONLY_AST,
                 incomplete_input=False,
             )
-        except (SyntaxError, OverflowError, ValueError):
+        except SyntaxError as e:
+            if "pip install" in source:  # maybe use a more general regex?
+                e.add_note(
+                    "Did you mean to run the 'pip' command?."
+                    " If so, <insert tip message here>"
+                )
+            self.showsyntaxerror(filename, source=source)
+            return False
+        except (OverflowError, ValueError):
             self.showsyntaxerror(filename, source=source)
             return False
         if tree.body:

@adqm
Copy link

adqm commented Jul 7, 2025

I teach Python at the university level, and this is a pretty common issue, so I think this would be a good addition! Maybe checking if "pip install" shows up anywhere in the source string is too general, though? That would fire on input like print('pip install"), which I'm not sure is helpful.

I feel like this might want to be a more specific check, like matching the source string against something like ^\s*(py(thon3?)? -m pip|pip3?) install. I suppose you could even check for a variant involving sys.executable, though that's probably overkill (since the likely culprit here is copy/pasting general instructions from somewhere).

@ichard26
Copy link
Author

ichard26 commented Jul 7, 2025

Ah crap, I forgot that force-pushing is frown upon here. Sorry about that!

@brianschubert thanks for the suggestion. Implementing this via the new REPL is much cleaner! I've updated my patch to use your approach.

@ichard26 ichard26 changed the title gh-72327: Suggest using system terminal for pip install in REPL gh-72327: Suggest using system terminal for pip install in PyREPL Jul 7, 2025
@ichard26 ichard26 marked this pull request as ready for review July 7, 2025 20:59
@jason-fine
Copy link

It may be nice to include "or pip3" depending on which version they want to use.

@ichard26
Copy link
Author

ichard26 commented Jul 9, 2025

I really don't want to get into the weeds of how to invoke pip (because the answer is "it depends" and I don't want to make this more complicated). The point of saying "try the 'pip' command" is that it can't simply be copied. The onus is on the user to invoke pip appropriately for their system. In most cases, I expect them to re-copy and paste what they just pasted/typed in.

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
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):
Copy link
Contributor

@ncoghlan ncoghlan Jul 14, 2025

Choose a reason for hiding this comment

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

Suggested change
if re.match(r"^\s*(py(thon3?)? -m pip|pip3?) install.*", source):
if re.match(r"^\s*(pip3?|py(thon3?)? -m pip) install.*", source):

Pretty sure that keeps the overall regex meaning the same, while making the optional suffix handling style more internally consistent.

Edit: changed the suggestion to flip the order of the branches, since my first suggestion of removing the pattern alternation entirely was wrong (as per the comments below)

Copy link

@adqm adqm Jul 14, 2025

Choose a reason for hiding this comment

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

I think this would actually change the meaning of the regex, and that the updated version won't match a regular pip install foo, only ones that use the python3 -m pip form. The | in the original regex isn't saying "pip or pip3" for that one little part, but rather that the whole parenthesized bit there must match "py(thon3?)? -m pip or pip3?".

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, you're right, I always forget how broad the scope of | is when mentally parsing regexes.

Copy link
Contributor

Choose a reason for hiding this comment

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

Reworked the suggestion to instead be to put the simpler branch first. I don't think that's a merge blocker though.

Copy link
Contributor

@ncoghlan ncoghlan left a comment

Choose a reason for hiding this comment

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

LGTM, but I'll leave it open for a bit due to that potential slight tweak to the regex layout.

@adqm
Copy link

adqm commented Jul 14, 2025

Do we also maybe want to check for variants that start try to use sudo? So, like ^\s*(sudo )?(pip3?|py(thon3?)? -m pip) install.*?

@ichard26
Copy link
Author

I've never encountered someone doing that. Have your students done that before? @adqm

@adqm
Copy link

adqm commented Jul 14, 2025

I'm not sure how common it is anymore, but I feel like it used to be fairly normal to suggest using sudo pip install to install things to your system's Python installation, so I wouldn't be surprised if the version with sudo was something that might still get copy/pasted in. I don't feel too strongly about this, though, just a thought.

Separately, I do agree that @ncoghlan's rephrasing of the regex is an improvement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy