Posts filed under ‘Uncategorized’

Python REPL Enhancement

This tip comes from here.

You can get tab completion and command history similar to that available in bash itself by creating registering a start up script for python. Create the following as ~/.pythonstartup

# python startup file
import readline
import rlcompleter
import atexit
import os

# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try:
	readline.read_history_file(histfile)
except IOError:
	pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

Then add the following to you ~/.bash_profile

export PYTHONSTARTUP=~/.pythonstartup

Now you can use tab to autocomplete and use tab-tab to show suggestions:

geoffs-mac:~ geoff$ python
Python 2.5 (r25:51918, Sep 19 2006, 08:49:13) 
[GCC 4.0.1 (Apple Computer, Inc. build 5341)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> pygame.display. <-- I pressed tab here after pyg and the after .dis, but you can't see it :) 
pygame.display.Info               pygame.display.get_caption
pygame.display._PYGAME_C_API      pygame.display.get_driver
pygame.display.__PYGAMEinit__     pygame.display.get_init
... and more ...

You can also use the up and down arrows to cycle through previous commands, including those from previous sessions.

January 20, 2009 at 1:07 pm


Calendar

May 2012
M T W T F S S
« Jan    
 123456
78910111213
14151617181920
21222324252627
28293031  

Posts by Month

Posts by Category


Follow

Get every new post delivered to your Inbox.