Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
ncoghlan merged 4 commits intopython:mainfromichard26:pip-repl-error
Jul 15, 2025

Conversation

ichard26
Copy link
Contributor

@ichard26ichard26 commentedJul 5, 2025
edited
Loading

Users new to Python packaging often try to use pip from the REPL only to be met with a confusingSyntaxError. 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

notatallshaw reacted with thumbs up emoji
Users new to Python packaging often try to use pip from the REPL only tobe met with a confusing SyntaxError. If this happens, guide the user touse 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 whenawait is used at the top-level:

exceptSyntaxErrorase:
ife.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.pyindex 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 commentedJul 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 likeprint('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 involvingsys.executable, though that's probably overkill (since the likely culprit here is copy/pasting general instructions from somewhere).

ichard26 reacted with thumbs up emoji

@ichard26
Copy link
ContributorAuthor

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.

@ichard26ichard26 changed the titlegh-72327: Suggest using system terminal for pip install in REPLgh-72327: Suggest using system terminal for pip install in PyREPLJul 7, 2025
@ichard26ichard26 marked this pull request as ready for reviewJuly 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
ContributorAuthor

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 itcan'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.

adqm, jason-fine, hugovk, and ncoghlan reacted with thumbs up emoji

Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Copy link
Contributor

@ncoghlanncoghlan 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

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

@ichard26
Copy link
ContributorAuthor

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

@adqm
Copy link

I'm not sure how common it is anymore, but I feel like it used to be fairly normal to suggest usingsudo pip install to install things to your system's Python installation, so I wouldn't be surprised if the version withsudo 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.

@ncoghlan
Copy link
Contributor

For thesudo question, I'd like to wait and see if we can get a sense of how this initial custom error plays out.

Thesudo case is a much harder situation to provide a good error message for, as typing thesudo version into the REPL instead of the terminal can actually save people from making a system killing mistake, so wedon't want to encourage them to try that specific command in the terminal.

@ncoghlanncoghlanenabled auto-merge (squash)July 15, 2025 13:59
@ncoghlanncoghlan merged commitbe02e68 intopython:mainJul 15, 2025
49 checks passed
@ichard26ichard26 deleted the pip-repl-error branchJuly 15, 2025 14:27
@ichard26
Copy link
ContributorAuthor

Thanks for the review@ncoghlan!

ncoghlan reacted with thumbs up emoji

@ncoghlan
Copy link
Contributor

Thanks for updating the PR to the new REPL! The previous PR stalled, I think mostly because the potential consequence of changing thesite.enablerlcompleter API weren't clear, so being able to avoid that concern this time around made a big difference.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@adqmadqmadqm left review comments

@hugovkhugovkhugovk left review comments

@ncoghlanncoghlanncoghlan approved these changes

@FFY00FFY00Awaiting requested review from FFY00

@pablogsalpablogsalAwaiting requested review from pablogsalpablogsal is a code owner

@lysnikolaoulysnikolaouAwaiting requested review from lysnikolaoulysnikolaou is a code owner

@ambvambvAwaiting requested review from ambvambv is a code owner

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

6 participants
@ichard26@brianschubert@adqm@jason-fine@ncoghlan@hugovk

[8]ページ先頭

©2009-2025 Movatter.jp