Python Error Message – Why It Appears When Typing Nonsense Commands

bashpythonUbuntu

Whenever I type any "nonsense" command, this python error message is generated. Normal commands work fine. Any idea how to debug this?

$ somenonexistingcommand
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site.py", line 553, in <module>
    main()
  File "/usr/local/lib/python2.7/site.py", line 535, in main
    known_paths = addusersitepackages(known_paths)
  File "/usr/local/lib/python2.7/site.py", line 268, in addusersitepackages
    user_site = getusersitepackages()
  File "/usr/local/lib/python2.7/site.py", line 243, in getusersitepackages
    user_base = getuserbase() # this will also set USER_BASE
  File "/usr/local/lib/python2.7/site.py", line 233, in getuserbase
    USER_BASE = get_config_var('userbase')
  File "/usr/local/lib/python2.7/sysconfig.py", line 535, in get_config_var
    return get_config_vars().get(name)
  File "/usr/local/lib/python2.7/sysconfig.py", line 434, in get_config_vars
    _init_posix(_CONFIG_VARS)
  File "/usr/local/lib/python2.7/sysconfig.py", line 298, in _init_posix
    raise IOError(msg)
IOError: invalid Python installation: unable to open /usr/include/python2.7/pyconfig.h (No such file or directory)
$ echo this works fine, however
this works fine, however
$

EDIT – after fixing my /usr/bin/python, I now get this different python error message:

$ yetanothernonexistingcommand
Traceback (most recent call last):
  File "/usr/lib/command-not-found", line 10, in <module>
    import CommandNotFound
ImportError: No module named CommandNotFound

Somehow, python is being run whenever I mistype a command.

Best Answer

Ok, that makes things a bit clearer. command-not-found is a python program, which runs when your command is not something found on the system. (Its function is to suggest alternatives and corrections in case of mistyping etc.) See /usr/bin/command-not-found. It is trying to import the CommandNotFound module and is unable to, clearly pointing to a screwed up python installation. I'm not that familar with command-not-found, but I think fixing your Python installation will make the problem go away.

Just to elaborate a bit, what is probably happening is that the command-not-found module is located somewhere where your default python isn't looking for it. A path problem, basically.

Debug suggestions:

1) To start with, what is the output from

$ which python

and what does package/installation does that file belong to?

2) What is the output for your installation corresponding to the code below? The path here is this python's import path.

$ python
Python 2.6.6 (r266:84292, Dec 27 2010, 00:02:40) 
[GCC 4.4.5] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/local/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/pymodules/python2.6/gtk-2.0', '/usr/lib/python2.6/dist-packages/wx-2.8-gtk2-unicode']
Related Question