How do I fix "/usr/bin/python: No such file or directory" when entering an invalid command in bash?

8,990

Turns out I was overriding the command_not_found_handle function (a bash feature) with something that called python directly in my bashrc:

# if the command-not-found package is installed, use it
if [ -x /usr/lib/command-not-found ]; then
    function command_not_found_handle {
        # check because c-n-f could've been removed in the meantime
        if [ -x /usr/lib/command-not-found ]; then
           /usr/bin/python /usr/lib/command-not-found -- $1
           return $?
        else
           return 127
        fi
    }
fi

This code is no longer necessary (was it ever?) and removing it fixed my problem -- now the old "can be found in the following packages" output again.

For completeness: If I wanted to skip the python code and simple output that the command doesn't exist, I could put this in my bashrc:

function command_not_found_handle {
    echo Command not found: $1
    return 127
}
Share:
8,990

Related videos on Youtube

user941105
Author by

user941105

(my about me is no longer blank)

Updated on September 18, 2022

Comments

  • user941105
    user941105 over 1 year

    I just installed Ubuntu 18.04.4 LTS on Windows under Windows Subsystem For Linux (WSL). When I run a command that doesn't exist in my bash terminal (for example if I accidentally add an extra s to ls), I get this error:

    $ lss
    -bash: /usr/bin/python: No such file or directory
    

    The error's right, I only have python3 installed:

    $ ls /usr/bin/python*
    /usr/bin/python3           /usr/bin/python3-jsonpatch    /usr/bin/python3-jsonschema  /usr/bin/python3.6m
    /usr/bin/python3-jsondiff  /usr/bin/python3-jsonpointer  /usr/bin/python3.6           /usr/bin/python3m
    

    From previous use of Ubuntu, I remember that the message should be "The program 'lss' can be found in the following packages:" I don't need that message anymore and I don't want to install python2 just to get it working (this question has answers for solving the problem by installing py2).

    Is it possible to fix this without installing python2, symlinking python 3 as 2, or becoming perfect and never making typos again?

    Ideally, I could port the "command doesn't exist" script to python3 or disable it completely.

  • user941105
    user941105 about 4 years
    Sorry for the confusion. lss was a throwawy example. I'm talking about every single invalid command I enter. If I typo a command (like lss or aptatude) or use something that I haven't yet installed (eclipse), I get that error. On older versions of ubuntu, it would tell me what package to install to get that command.