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:
- Create a
~/.pythonrcfile with the following content:try: import readline except ImportError: print "Module readline not available." else: import rlcompleter readline.parse_and_bind("tab: complete") - Put this somewhere in your
~/.bashrcexport 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?
Tags: debian
July 6th, 2008 at 11:08 pm
Thanks for this! I didn’t know about it either.
July 6th, 2008 at 11:20 pm
I use a more complicated version:
import os.path, atexit
try:
import readline
except ImportError:
pass
else:
import rlcompleter
class irlcompleter(rlcompleter.Completer):
def complete(self, text, state):
if text == “”:
readline.insert_text(’\t’)
return None
else:
return rlcompleter.Completer.complete(self,text,state)
#you could change this line to bind another key instead tab.
readline.parse_and_bind(”tab: complete”)
readline.set_completer(irlcompleter().complete)
# Restore our command-line history, and save it when Python exits.
historyPath = os.path.expanduser(”~/.pyhistory”)
readline.read_history_file(historyPath)
atexit.register(lambda x=historyPath: readline.write_history_file(x))
It’s much like yours, except that you can still use tab at the beginning of a line to insert an indent, and it saves your python command history between sessions, much like bash does.
July 6th, 2008 at 11:22 pm
Bah, hat u wordpress.
Anyway, I’m sure you can figure out what the proper indentation was.
July 7th, 2008 at 12:05 am
use ipython and you get this in color ;-P
July 7th, 2008 at 1:46 am
I’m with nion, why not just use ipython? I’m always shocked when I see experienced Pythonistas using python’s interactive mode instead of ipython.
Is there some compelling reason *not* to use ipython?
July 7th, 2008 at 3:54 pm
schmichael: Only when ipython is not installed.
April 21st, 2009 at 6:43 pm
That is awesome, and I can’t believe I didn’t know about ipython you have change my life nion. Thank you
July 12th, 2009 at 6:40 am
Check out this extended version of your python tab completion trick completely with full on history support:
http://www.digitalprognosis.com/opensource/scripts/pythonrc
It is an extension of ScrewTape’s idea.