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

4

/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,236
  • 4
  • 48
  • 98
  • I think that is what happened. I always use different python packages with virtualenv sandboxing, but recently I might have accidentally installed some version of python globally. The outputs are as follows:

    type python 3: python3 is hashed (/usr/bin/python3); python3 --version: Python 2.7.6

    So looks like python3 is actually a link to python 2. Should I use the reinstallation command?

    – Ishtiaque Khan Nov 23 '17 at 21:53
  • @IshtiaqueKhan OK, looks like you've accidentally installed python2.7 at /usr/bin/python3.4. You might want to run python --version to check that it's unaffected. – wjandrea Nov 23 '17 at 22:46
  • readlink /usr/bin/python3.4 does not print anything. I have compiled all this into the question. – Ishtiaque Khan Nov 24 '17 at 07:54
  • 1
    @IshtiaqueKhan I forgot to mention, don't restart your computer! Some parts of the GUI depend on Python 3. You will probably want to keep the terminal open too. – wjandrea Nov 24 '17 at 14:50
  • @IshtiaqueKhan You could try reinstalling python3-minimal, but I expect it will throw an error too. – wjandrea Nov 24 '17 at 14:58
  • If it's only the executable that's the problem, you could download the python3-minimal package, extract it somewhere, then copy the executable to /usr/bin/python3. (Sorry, I tried to start a chat, but it's not working properly). – wjandrea Nov 24 '17 at 15:14
  • @IshtiaqueKhan Thanks for the accept, and glad you got it fixed! How did you end up fixing it? That info may help other folks in the future. – wjandrea Nov 24 '17 at 20:29
  • You are right, restarting totally messed things up and I lost my dashboard and a lot of UI. So I tried to install python3 minimal, but ended up with the dependency requirement cycle python3, python3 minimal and some other packages. So the only last attempt left was to re-build python3 from source. I thought I would be much better off by backing up my data and getting rid of Ubuntu 14 altogether and installing Ubuntu 16. Thanks a lot, you were on point though. – Ishtiaque Khan Nov 24 '17 at 20:31