How does CommandNotFound Work?

Usually, when we typed a non-exist command, an error message"command not found" occurs. But, some times shell is smart than this by giving more infomation:

[alien@ubuntu~/tmp]:)zsh
The program 'zsh' can be found in the following packages:
 * zsh-beta
 * zsh
Ask your administrator to install one of them
-bash: zsh: command not found
[alien@ubuntu~/tmp]:(john
The program 'john' is currently not installed.  To run 'john' please ask your administrator to install the package 'john'
-bash: john: command not found
[alien@ubuntu~/tmp]:(which zsh
[alien@ubuntu~/tmp]:(which john

I find the code

print >>sys.stderr, _("The program '%s' is currently not installed. ") % command

in /usr/share/pyshared/CommandNotFound/CommandNotFound.py

I am wondering how bash make use of the CommandNotFound.py to print the smart error message?

Unless you've got something weird in your profile that runs python scripts every time you get a command not found error, I suspect that's a python thing and not a bash one.

---------- Post updated at 09:43 AM ---------- Previous update was at 09:40 AM ----------

First hit for 'not found' in man bash:

       If the name is neither a shell function nor a builtin, and contains  no
       slashes,  bash  searches  each element of the PATH for a directory con-
       taining an executable file by that name.  Bash uses  a  hash  table  to
       remember  the  full pathnames of executable files (see hash under SHELL
       BUILTIN COMMANDS below).  A full search of the directories in  PATH  is
       performed  only  if the command is not found in the hash table.  If the
       search is unsuccessful, the shell searches for a defined shell function
       named command_not_found_handle.  If that function exists, it is invoked
       with the original command and the original command's arguments  as  its
       arguments,  and  the  function's exit status becomes the exit status of
       the shell.  If that function is not defined, the shell prints an  error
       message and returns an exit status of 127.

Here is a good overview of command-not-found: Bash: Handling Command Not Found | Linux Journal.

How it is actually implemented varies by distribution.

Very enlightening, thank you all.