cpython icon indicating copy to clipboard operation
cpython copied to clipboard

Give better errors for OS commands, like 'pip', in REPL, script

Open ncoghlan opened this issue 9 years ago • 8 comments

BPO 28140
Nosy @terryjreedy, @ncoghlan, @aroberge, @stevendaprano, @1st1, @tomviner, @pablogsal, @iritkatriel, @AlexWaygood
PRs
  • python/cpython#8536
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2016-09-14.04:40:03.485>
    labels = ['interpreter-core', 'type-feature', '3.11']
    title = "Give better errors for OS commands, like 'pip', in REPL, script"
    updated_at = <Date 2021-11-28.19:44:25.495>
    user = 'https://github.com/ncoghlan'
    

    bugs.python.org fields:

    activity = <Date 2021-11-28.19:44:25.495>
    actor = 'tomviner'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Interpreter Core']
    creation = <Date 2016-09-14.04:40:03.485>
    creator = 'ncoghlan'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 28140
    keywords = ['patch']
    message_count = 8.0
    messages = ['276373', '276506', '276510', '322565', '407201', '407202', '407213', '407226']
    nosy_count = 9.0
    nosy_names = ['terry.reedy', 'ncoghlan', 'aroberge', 'steven.daprano', 'yselivanov', 'tomviner', 'pablogsal', 'iritkatriel', 'AlexWaygood']
    pr_nums = ['8536']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue28140'
    versions = ['Python 3.11']
    

    ncoghlan avatar Sep 14 '16 04:09 ncoghlan

    A problem we're starting to see on distutils-sig is folks trying to type pip commands into the Python REPL rather than their system shell, and getting cryptic syntax errors back:

    >>> pip install requests
      File "<stdin>", line 1
        pip install requests
                  ^
    SyntaxError: invalid syntax
    >>> python -m pip install requests
      File "<stdin>", line 1
        python -m pip install requests
                    ^
    SyntaxError: invalid syntax
    

    This may be amenable to a similar solution to the one we used to give a custom error message for "print ":

    >>> print foo
      File "<stdin>", line 1
        print foo
                ^
    SyntaxError: Missing parentheses in call to 'print'
    

    That code currently checks for "print " and "exec ", so it would be a matter of adding another special case that looked for "pip install " appearing anywhere in the string that's failing to compile (it can't be limited to the start as it may be an attempt to invoke pip via "-m")

    ncoghlan avatar Sep 14 '16 04:09 ncoghlan

    Paul Moore pointed out on distutils-sig that since this is mainly desired for the REPL, we can put the logic in the default excepthook rather than into the SyntaxError constructor the way we had to for "print" and "exec":

        def excepthook(typ, value, traceback):
            if typ is SyntaxError and "pip install" in value.text:
                print("'pip install' found in supplied text")
                print("Try running this from a system command prompt")
                return
            sys.__excepthook__(typ, value, traceback)
    

    ncoghlan avatar Sep 15 '16 03:09 ncoghlan

    Given that this can be done with just an excepthook change, I'm going to suggest we make the change for 3.5 and 2.7 as well.

    ncoghlan avatar Sep 15 '16 03:09 ncoghlan

    I am looking at this, as part of the EuroPython 2018 sprint.

    On 3.11:

    >>> pip install requests
      File "<stdin>", line 1
        pip install requests
        ^^^^^^^^^^^
    SyntaxError: invalid syntax. Perhaps you forgot a comma?
    

    iritkatriel avatar Nov 28 '21 12:11 iritkatriel

    Similar discussion in a newer issue: https://bugs.python.org/issue45721

    AlexWaygood avatar Nov 28 '21 12:11 AlexWaygood

    I closed bpo-45721, which has additional comments, as a duplicate of this.

    'print foo' now results in "SyntaxError: Missing parentheses in call to 'print'. Did you mean print(...)?"

    terryjreedy avatar Nov 28 '21 17:11 terryjreedy

    I've updated my pull request from 3 years ago. Fixed merge conflicts and addressed all comments.

    https://github.com/python/cpython/pull/8536