Command-line tab completion in standard Python shellTo 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
try:
import readline
except ImportError:
print "Module readline not available."
else:
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
other 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+"'), \
open('"+historyTmp+"').readlines())[:])+'####<<<<<<<<<<'"+endMarkerStr
readline.parse_and_bind('tab: complete')
readline.parse_and_bind('\C-w: "'+saveMacro+'"')
def save_history(historyPath=historyPath, endMarkerStr=endMarkerStr):
import readline
readline.write_history_file(historyPath)
# 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):
readline.read_history_file(historyPath)
atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath
del historyTmp, endMarkerStr, saveMacro
Additionaly you need to ~# export PYTHONSTARTUP=/root/.pystartup. Then you will have:
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
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 (ip) on 05 Nov 2005, 12:42
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 (ip) on 26 Feb 2006, 19:46
There are some problems with Readline support in Python when you're compiling it on Mac OS X. I added an article about this:
other article
Nice but how would you create intended blocks in function or class definition now? With spacebar?
• wrote Ivan (ip) on 25 Mar 2008, 09:17
Much easier method:
http://ipython.scipy.org/
But when I'm forced to use the standard interpreter, I'll try this.
• wrote Ehh (ip) on 23 May 2009, 05:32
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
Then:
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 (ip) on 08 Oct 2005, 17:15