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?

blogroll

social