Command-line tab completion in standard Python shell

Monday 22 November 2004, 21:12:00 | python

To enable 'tab-completion' on the command line of the default Python shell, put this in ~/.pythonrc, and setup the PYTHONSTARTUP environment variable to point to it: export PYTHONSTARTUP=~/.pythonrc

# ~/.pythonrc
# enable syntax completion
    import readline
except ImportError:
    print "Module readline not available."
    import rlcompleter
    readline.parse_and_bind("tab: complete")

It needs the GNU readline library and so probably doesn't work on Windows. On Mac OS X there are some issues with readline too, see →Readline on Mac OS X.

For even fancier stuff, use the following startup file: .pystartup in your home-dir

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
# Store the file in ~/.pystartup, and set an environment variable to point
# to it, e.g. "export PYTHONSTARTUP=/max/home/itamar/.pystartup" in bash.
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")
historyTmp = os.path.expanduser("~/")

endMarkerStr= "# # # histDUMP # # #"

saveMacro= "import readline; readline.write_history_file('"+historyTmp+"'); \
    print '####>>>>>>>>>>'; print ''.join(filter(lambda lineP: \
    not lineP.strip().endswith('"+endMarkerStr+"'),  \

readline.parse_and_bind('tab: complete')
readline.parse_and_bind('\C-w: "'+saveMacro+'"')

def save_history(historyPath=historyPath, endMarkerStr=endMarkerStr):
    import readline
    # Now filter out those line containing the saveMacro
    lines= filter(lambda lineP, endMarkerStr=endMarkerStr:
                      not lineP.strip().endswith(endMarkerStr), open(historyPath).readlines())
    open(historyPath, 'w+').write(''.join(lines))

if os.path.exists(historyPath):


del os, atexit, readline, rlcompleter, save_history, historyPath
del historyTmp, endMarkerStr, saveMacro

Additionaly you need to ~# export PYTHONSTARTUP=/root/.pystartup. Then you will have:

People have replied:

Mario Ruggier —

2005-10-08 17:15:00

How do I install the readline library for python on OS X?

I have downloaded:


$ ./configure --host=powerpc # it could not guess system type
$ make 
$ sudo make install

But, when I do:

$ python
Python 2.4.2 (#1, Oct  3 2005, 16:22:31) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named readline
>>> import rlcompleter
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/local/lib/python2.4/", line 42, in ?
    import readline
ImportError: No module named readline

Where would the problem be?

Irmen de Jong

2005-10-09 00:13:00

Mario, I don't know why you don't have readline support in your Python build. Did you re-build the modules after installing the readline library?

It works for me, Python 2.4.2 compiled on Mac OS X 10.4. I've got readline installed as part of Fink, though -- not by itself:

[~] $ python
Python 2.4.2 (#2, Oct  7 2005, 17:43:19)
[GCC 4.0.0 20041026 (Apple Computer, Inc. build 4061)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import readline
>>> import rlcompleter

[~] $ fink list readline5
Information about 4868 packages read in 2 seconds.
 i   readline5        5.0-4        Comfortable terminal input library
 i   readline5-sh...  5.0-4        Comfortable terminal input library
[~] $

Mario Ruggier —

2005-11-05 12:42:00

It does work now after I rebuild python 2.4.2, and re-install, on OS X 10.3, thanks a lot.

Btw, I am doing a framework python install, in this way:

$ cd <download dir for Python-2.4.2>
$ env MACOSX_DEPLOYMENT_TARGET=10.3 ./configure --enable-framework
$ make
$ sudo make frameworkinstall


2006-01-24 20:14:00

Just wanted to say, this is incredibly useful, thanks!


2006-02-26 19:46:00

on 10.3, i was able to do this (all on one line) from the terminal to get readline. Got this off someone else's webpage...

curl -s | gzip -d -c | sudo cat > /Library/Python/2.3/

thanks for the killer tips!

Irmen de Jong

2006-05-10 16:36:00

There are some problems with Readline support in Python when you're compiling it on Mac OS X. I added an article about this: →Readline on Mac OS X


2008-03-25 09:17:00

Nice but how would you create intended blocks in function or class definition now? With spacebar?


2009-05-23 05:32:00

Much easier method:

But when I'm forced to use the standard interpreter, I'll try this.