Spinsels op het web
actions » SearchLogin 382 articles • 19 Apr 2014

Article with comments

Monday, 22 Nov 2004

permalink Command-line tab completion in standard Python shell

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 remoteother article.

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("~/.pyhisttmp.py")

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:

  • Pressing tab completes the input like common unix-shells.
  • Pressing CTRL-w gives You an python code to print the history-file (or parts of it -- there's a [:] in the command that You can replace by [-50:]; which will give you the last 50 lines)
• Wrote irmen at 21:12 (edited 1×, last on 08 May 2006) | read 18806× | Add comment

Comments (8)

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

I have downloaded: ftp://ftp.gnu.org/pub/gnu/readline/readline-4.0.tar.gz


$ ./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/rlcompleter.py", line 42, in ?
    import readline
ImportError: No module named readline

Where would the problem be?

• wrote Mario Ruggier on 08 Oct 2005, 17:15  permalink

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
[~] $
• wrote irmen on 09 Oct 2005, 00:13  permalink

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
• wrote Mario Ruggier on 05 Nov 2005, 12:42  permalink

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

• wrote Bryon on 24 Jan 2006, 20:14  permalink

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 http://www.pycs.net/bbum/2004/1/21/readline.so.gz | gzip -d -c | sudo cat > /Library/Python/2.3/readline.so

thanks for the killer tips!

• wrote plooney on 26 Feb 2006, 19:46  permalink

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

• wrote irmen on 10 May 2006, 16:36  permalink

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

• wrote Ivan on 25 Mar 2008, 09:17  permalink

Much easier method:


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

• wrote Ehh on 23 May 2009, 05:32  permalink

Write a comment

Your name  
E-mail   (only visible for blog owner)

[b] [i] [u] [tt] [center] [code] [quote] [url] [url=] [img] [@] [@@] [@:]
detailed help about markup
You must answer the following to be able to submit.
How much is three plus twelve?  
[Captcha Image] Type the letters you see in the image.
(Unreadable? Click on it for another one)

Process times: page=0.013 request=0.021 cpu=0.022