Friday, August 21, 2009

Tab Auto-Completion in Python Shell

Strangely enough, I discovered that I do not get bash-like auto-completion in my python interactive shell.

Looking up helped.

Just need to have a file called .pythonrc in your home directory containing the following:


#!/usr/bin/python
import os
import sys

try:
import readline
except ImportError:
print "Module readline not available."
else:
import rlcompleter
readline.parse_and_bind("tab: complete")


Apparently, that was not enough. I also needed to add the following line to my .bashrc file (in my home directory again).

export PYTHONSTARTUP=~/.pythonrc


That's all. Open a new shell and start the python shell.

And enjoy auto-completion!

11 comments:

  1. Or you can use ipython. :-)

    ReplyDelete
  2. We have enabled this in Mandriva by default without people complaining since some years, so I think this would be safe and useful to be enabled on fedora ( and in other distribution too ).

    ReplyDelete
  3. It is by default there in openSuse

    ReplyDelete
  4. Why not convince upstream python to include this as a default so that its expected behavior for everyone? Then there isnt a patchwork of expected behavior, which is the current situation.

    ReplyDelete
  5. https://bugzilla.redhat.com/show_bug.cgi?id=518653

    ReplyDelete
  6. Just studying python at cambridge so thanks for the info.

    buy my car

    ReplyDelete
  7. We have enabled this in Mandriva by default, love python !

    sell my car

    ReplyDelete
  8. Thanks for the tips about Auto compilation. Keep going. I follow you.
    usedcarsforsale

    ReplyDelete
  9. The code looks the same as Irmen's blog from 2004 ( http://www.razorvine.net/blog/user/irmen/article/2004-11-22/17 )

    But why include import os and import sys and not use them?

    ReplyDelete