3

When I run a wrong command on my Ubuntu terminal, instead of echo-ing "command not found", the terminal does nothing, and when I press Ctrl+C it is echo-ing some python exception stack trace. Is something wrong? How can I fix this?

For example, when I enter "sdf", it does nothing. "sdf" is not a valid command.

When I enter an invalid command, terminal does nothing.

But when I press Ctrl+C, it prints this python exception stack trace.

When I press ctr-C it is echo-ing some python exception stack

The stack trace is different every time.

I am using Ubuntu 14.04, so python2 is the default

$ type python python2 python3
python is /usr/bin/python
python2 is /usr/bin/python2
python3 is hashed (/usr/bin/python3)
$ readlink /usr/bin/python /usr/bin/python2 /usr/bin/python3
python2.7
python2.7
python3.4
$ python --version
Python 2.7.6
$ python3 --version
Python 2.7.6
$ readlink /usr/bin/python3.4
$ 
  • sudo apt-get install --reinstall python did not help.
  • sudo apt-get install --reinstall python3 threw an error

Update:
So after trying a lot of approaches, I ended up reinstalling Ubuntu. Follow wjandrea's answer and the comments, seem to be on point in identifying the problem.

1 Answers1

5

/etc/bash.bashrc defines a function command_not_found_handle, which calls /usr/lib/command-not-found, which is a Python 3 script. This handler is called for commands that Bash can't find.

So as a bandage fix, you can unset the handler:

unset -f command_not_found_handle

Update 2:

After some discussion with OP, it turns out the issue is caused by a Python 2.7 executable accidentally placed at /usr/bin/python3.4. (So my first update was not very useful, but it's in revision 4 if you want to read it). If this happens to you, don't restart your computer! Some parts of the GUI depend on Python 3. You will probably need to keep the terminal open too.

BTW, this explains the infinite loop when calling an unknown command at the Bash prompt. /usr/lib/command-not-found has this section in it:

if sys.version < '3':
    # We might end up being executed with Python 2 due to an old
    # /etc/bash.bashrc.
    import os
    if "COMMAND_NOT_FOUND_FORCE_PYTHON2" not in os.environ:
        os.execvp("python3", [sys.argv[0]] + sys.argv)

Which means when it gets executed by Python 2, it calls python3, but since python3 is actually Python 2, the process repeats.

Update 3:

OP ended up reinstalling Ubuntu, but I was curious so I opened a VM, caused a similar issue (if not the same issue), and fixed it.

  1. Caused the problem

    sudo cp /usr/bin/python2.7 /usr/bin/python3.4
    
  2. Confirmed the problem

    • Ran python3 --version, got Python 2.7.6
    • Tried running sdf, had to press Ctrl+C to stop the loop
  3. Fixed it:

    sudo apt-get install --reinstall python3.4-minimal
    

    The package python3.4-minimal provides the Python 3.4 executable itself. All the other packages I checked (python3, python3.4, python3-minimal) depend on python3.4-minimal for that reason.

(OP and I stumbled through this solution the first time around. For more details about what I tried, what else I messed up, and how I fixed it, read revision 9 of this answer.)

wjandrea
  • 14,504