Interactive Python Command Completion

about | archive


[ 2005-March-31 16:21 ]

One of Python's great features is that it can be used interactively. I just discovered that it also supports command completion via the rlcompleter module. If you are on Mac OS X, you will first need to install the readline module (run this command: python `python -c "import pimp; print pimp.__file__"` -i readline). To enable tab completion execute the following code:

import rlcompleter
import readline
readline.parse_and_bind("tab: complete")

You can set up Python to do this every time you start the interactive interpreter.

  1. Set the PYTHONSTARTUP environment variable to ~/.pythonrc.py. You probably want to set this in your ~/.profile or the equivalent configuration file to make it permanent.
  2. Edit ~/.pythonrc.py and add the three lines above
  3. You may optionally wish to add the lines del readline and del rlcompleter to leave a pristine namespace

If you really like the interactive console, you may wish to play with Holger Krekel's rlcompleter2 module. It displays much more information, like the type and value of objects and the docstrings for modules and functions.