code: def setup_completion(shell):
      
  
      # Handle special case of importing pyreadline on Windows
      # See: http://stackoverflow.com/questions/6024952/readline-functionality-on-windows-with-python-2-7
      import glob
      try:
          import readline
      except ImportError:
          import pyreadline as readline
  
      def _complete(text, state):
          buf = readline.get_line_buffer()
          if buf.startswith('help ') or " " not in buf:
              return [x for x in shell.valid_identifiers() if x.startswith(text)][state]
  
          return (glob.glob(os.path.expanduser(text)+'*')+[None])[state]
  
      readline.set_completer_delims(' \t\n;')
      # Handle Mac OS X special libedit based readline
      # See: http://stackoverflow.com/questions/7116038/python-tab-completion-mac-osx-10-7-lion
      if readline.__doc__ is not None and 'libedit' in readline.__doc__:
          readline.parse_and_bind("bind ^I rl_complete")
      else:
          readline.parse_and_bind("tab: complete")
  
      readline.set_completer(_complete)
docs: Setup readline to tab complete in a cross platform way.