Saturday, August 16, 2008

[TAB] in Python interpreter..

I always use bash on terminal and often find the tab-completion feature useful. Bet on it, without that, life would be horrible..

Yesterday, I and Kunal were working on some pythonic stuff. It is regarding pyslide story. We needed some kind of xmlparser in pyslide as python-xml is to be removed. I don't have any idea of what is XML and so was googling around today. While trying for some stuff with Python shell, I realized that their is no tab-completion in Python interactive shell. And it would be of great help. So bumped on this in Python Docs. While searching for proper way to do tab completion, I saw a video on dgplug tv by Kushal.

Here's what I did to get tab-completion in Python's interactive shell..

1]Create a ~/.pythonrc file with following content.

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

2] Put the following line in ~/.bashrc file.
 export PYTHONSTARTUP=~/.pythonrc

Now, everytime you start the python interactive shell, the ~/.pythonrc file is executed and HURRAY [TAB] works in Python shell.. :-)


aamod@cowbee:~$ python

Python 2.5.2a0 (r251:54863, Jan 3 2008, 17:59:56)
[GCC 4.2.3 20071123 (prerelease) (Debian 4.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
os.pardir os.pathconf os.pathsep
os.path os.pathconf_names


Raza said...

Nice tip..thnkx for posting :)

Puneet said...

I wasnt even looking for this thing, but stumbling on this is a godsend.
Thanks a lot :)

Puneet said...

And by the way for those who are interested, the following code in your ~/.pythonrc would save your history and restore. Just like bash

import os
histfile = os.path.join(os.environ["HOME"], ".pyhist")
except IOError:
import atexit
atexit.register(readline.write_history_file, histfile)
del os, histfile

Anonymous said...

Who knows where to download XRumer 5.0 Palladium?
Help, please. All recommend this program to effectively advertise on the Internet, this is the best program!