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>
  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
  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.

  • 1
    @ripper: Interesting. A few stray observations. Does your .bashrc have anything connected with python in it? Do you get this same behavior with other user accounts? Why do you have python 2.7 installed locally? What is the default official python on this installation, and if not 2.7, do you have it installed? What ubuntu version is this? Mar 18, 2011 at 19:02
  • @Faheem - I don't see anything python related in .bashrc. I installed python locally myself. I know the machine has two other pythons installed as well. I have Ubuntu 10.10 installed.
    – ripper234
    Mar 18, 2011 at 19:07
  • This smells a bit like a python installation gone astray. What are the versions official system pythons installed, and why did you install 2.7 locally? Is it not one of the officially available versions? Mar 18, 2011 at 19:11
  • @Faheem - I don't understand your question. What do you mean by "official"? I needed python 2.7, so I installed it. I am the admin of this box. I installed python by doing make/make install, didn't do anything fishy (I think).
    – ripper234
    Mar 18, 2011 at 19:16
  • @ripper: Meaning, ubuntu provided it as a binary package. When you type python, which python do you get? Mar 18, 2011 at 19:19

7 Answers 7


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

    $ type python

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

  1. 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']

  • 3
    one possible solution is # apt-get remove command-not-found...
    – simon
    Mar 19, 2011 at 3:41
  • "Screwed up python installation" - any idea what exactly is missing. Is CommandNotFound a core python module, or can/should I install it separately?
    – ripper234
    Mar 19, 2011 at 14:19
  • @ripper: CommandNotFound is part of the command-not-found package, which must be installed on your machine already, else you wouldn't be seeing this message. However, you could easily verify that by querying the package manager. dpkg -l command-not-found . Your problem is that your default python installation is not seeing this module. I'll add some steps to debug above. Others please feel free to modify & correct. Mar 19, 2011 at 15:31
  • See this followup question - unix.stackexchange.com/questions/9711/…
    – ripper234
    Mar 20, 2011 at 8:03
  • the links to command-not-found seem broken (although it's hard to tell if "error" is the expected outcome when searching for "command-not-found"...) May 18, 2015 at 17:42

I had this same error after installing Python 3.5.0 on my Ubuntu 14.04 LTS ( which has a system python of version 3.4.0).

After I opened the /usr/lib/command-not-found, I realized this error is due to system executing this script using the newly installed python3.5.0, because installing Python3.5.0 creates leads the system to use it when you type in python3.

This error can be easily corrected by changing the first line from



  • This is the best answer imo. In my case it was a stupid decision to symlink python3 to a newer minor version, thinking compatibility shouldn't break. But yeah, lesson learned. Feb 12, 2019 at 0:09

I ran into this when I upgraded from the stock 2.6 that came with my ubuntu installation to the 3.2 python, with setting the default alternative to 3.2 rather than 2.6.

If you look at your /etc/bash.bashrc file there is a line that tells it to run this python script to look for alternatives in the repos. There is a package for it, however you cant remove the package once you've upgraded. I just simply moved the /usr/share/command-not-found and /usr/lib/command_not_found_handler and restarted my term and it works like good ol' bash: command not found.

  • 6
    If you want to disable the command_not_found handler, don't mess with /usr (that can make subsequent upgrades fail or be undone by subsequent upgrades). Instead, change /etc/bash.bashrc. Or disable this in your own ~/.bashrc with unset -f command_not_found_handle. Jul 4, 2011 at 8:31
  • In particular, /etc/bash.bashrc is provided by the bash package, so you could break updates to that package. Feb 24, 2015 at 11:42

Commenting out all the lines responsible for command-not-found in /etc/bash.bashrc solved the problem, which was created by switching Python versions.


The issue is with your $PATH environment variable. You've most likely messed it up. It should be similar to this:

$ echo $PATH

See this Linux Mint thread: http://forums.linuxmint.com/viewtopic.php?f=18&t=119561.

You can repair your $PATH in the shell with this command:

$ export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"

This is temporary! If the problem persists with a reboot then you've most likely hosed the $PATH in one of your environment setup files under /etc.


The "command-not-found" package is a linux utility; it responds to unknown commands at the command shell prompt, not just within python sessions. (I see there is also a python package of this name.)

It has python among its dependencies, i.e. it uses python when triggered; so that accounts for why python is invoked whenever you type a command that the shell can't find on your PATH.

I see there is an 'apt' package to install command-not-found in Linux; for Debian linux, this is cataloged at:



Just remove "command-not-found" using your package manager.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .