Tab Completion in Python’s Interactive Mode

While browsing the python docs, I accidentally stumbled upon this today. It describes how to get Tab completion in Python’s interactive shell:

  1. Create a ~/.pythonrc file with the following content:
    try:
        import readline
    except ImportError:
        print "Module readline not available."
    else:
        import rlcompleter
        readline.parse_and_bind("tab: complete")
    
  2. Put this somewhere in your ~/.bashrc
    export PYTHONSTARTUP=~/.pythonrc

The .pythonrc will be executed every time the Python interpreter is started but not when it executes a script.

How does it look like?

$ python
Python 2.5.2 (r252:60911, Jun 25 2008, 17:58:32)
[GCC 4.3.1] on linux2
Type “help”, “copyright”, “credits” or “license” for more information.
>>> l = []
>>> l.
l.__add__           l.__getslice__      l.__ne__            l.append
l.__class__         l.__gt__            l.__new__           l.count
l.__contains__      l.__hash__          l.__reduce__        l.extend
l.__delattr__       l.__iadd__          l.__reduce_ex__     l.index
l.__delitem__       l.__imul__          l.__repr__          l.insert
l.__delslice__      l.__init__          l.__reversed__      l.pop
l.__doc__           l.__iter__          l.__rmul__          l.remove
l.__eq__            l.__le__            l.__setattr__       l.reverse
l.__ge__            l.__len__           l.__setitem__       l.sort
l.__getattribute__  l.__lt__            l.__setslice__
l.__getitem__       l.__mul__           l.__str__
>>> l.

Very cool! Why is this hidden so deep in the docs and not enabled by default?

Tags:

8 Responses to “Tab Completion in Python’s Interactive Mode”

  1. Ondrej Says:

    Thanks for this! I didn’t know about it either.

  2. Screwtape Says:

    I use a more complicated version:

    import os.path, atexit

    try:
    import readline
    except ImportError:
    pass
    else:
    import rlcompleter

    class irlcompleter(rlcompleter.Completer):
    def complete(self, text, state):
    if text == “”:
    readline.insert_text(’\t’)
    return None
    else:
    return rlcompleter.Completer.complete(self,text,state)

    #you could change this line to bind another key instead tab.
    readline.parse_and_bind(”tab: complete”)
    readline.set_completer(irlcompleter().complete)

    # Restore our command-line history, and save it when Python exits.
    historyPath = os.path.expanduser(”~/.pyhistory”)
    readline.read_history_file(historyPath)
    atexit.register(lambda x=historyPath: readline.write_history_file(x))

    It’s much like yours, except that you can still use tab at the beginning of a line to insert an indent, and it saves your python command history between sessions, much like bash does.

  3. Screwtape Says:

    Bah, hat u wordpress.

    Anyway, I’m sure you can figure out what the proper indentation was.

  4. nion Says:

    use ipython and you get this in color ;-P

  5. schmichael Says:

    I’m with nion, why not just use ipython? I’m always shocked when I see experienced Pythonistas using python’s interactive mode instead of ipython.

    Is there some compelling reason *not* to use ipython?

  6. Ondrej Says:

    schmichael: Only when ipython is not installed. :)

  7. lotu Says:

    That is awesome, and I can’t believe I didn’t know about ipython you have change my life nion. Thank you

  8. Jeff Schroeder Says:

    Check out this extended version of your python tab completion trick completely with full on history support:

    http://www.digitalprognosis.com/opensource/scripts/pythonrc

    It is an extension of ScrewTape’s idea.

Leave a Reply