Skip to content

gh-72327: Suggest using system terminal for pip install in REPL #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

Draft
wants to merge 1 commit 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 <[email protected]>
@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:

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

Successfully merging this pull request may close these issues.

2 participants