Every Python developer knows that the Python shell can speed up the work, but not anybody know how fast it can be.
In Bash you can browse the history of previously typed commands and have autocompletion with the tab key. Why not having the same features in the Python shell too?
History
On UNIX-like systems, Python is shipped with a really useful module in the standard library: readline that, among other things, can be used to enable the history. Its usage is very simple: it just needs a file to read and write the history, just like Bash. Here’s how it should be used:
>>> import os
>>> import readline
>>> history_file = os.path.expanduser('~/.python_history')
>>> readline.read_history_file(history_file)
Note that the history file is not written automatically at exit: you ought to call write_history_file() when you finish to use the interpreter or, better, register a function with atexit:
>>> import atexit >>> atexit.register(readline.write_history_file, history_file)
Autocompletion
The readline module can be used for autocompletion too, but needs some help from a second module. In fact readline just binds the tab key, but the word completer itself is in rlcompleter.
>>> import rlcompleter
>>> readline.parse_and_bind('tab: complete')
RC file
We have understood how to enable both history and autocompletion, but does this mean that we should type the above commands every time? No, of course. When used in interactive mode, Python always looks into the PYTHONSTARTUP environment variable and executes the specified script, if any. So, you can put the code to enable history and autocompletion in a file (for example: ~/.pythonrc) and specify it in your PYTHONSTARTUP.
Using the RC file you also have the advantage that you can import the modules that you use frequently, so that you won’t have to import the manually in the interpreter. For example, here’s my own Python RC file:
import atexit
import os
import re
import readline
import rlcompleter
import socket
import _socket
import sys
import time
import timeit
history = os.path.expanduser('~/.python_history')
readline.read_history_file(history)
readline.parse_and_bind('tab: complete')
atexit.register(readline.write_history_file, history)
def t(*args):
return timeit.Timer(*args).timeit()
you might be interested in checking out bpython
http://www.bpython-interpreter.org/
There is a better one you might check out on my website:
http://www.digitalprognosis.com/opensource/scripts/pythonrc
(shameless plug)
Hey, nice post! This is really what I was looking for.
Or you could install IPython and have a much nicer python shell environment to run within than the basic built-in one.
You know about ipython, right?
If you like stuff like this you’ll love ipython: http://ipython.scipy.org/moin/
Have you tried ipython? It has all the features you mention here.
very nice, usefull
Yes, I already know IPython/BPython and I have already tried it. It’s really comfortable, but unfortunately it has some disadvantage for me.
The most important point is that I cannot install IPython on every computer I work on (e.g.: an host where I don’t have root access or with a low disk space). Instead, modifying an environment variable and creating a little script can be done almost everywhere.
However, thank you all for the suggestions!
My $PYTHONSTARTUP does three things:
1. set up history
2. set up tab-completion
3. set up a coloured prompt
You’ve covered 1 and 2, and here’s 3:
import os, sys
if os.getenv(‘TERM’) in (‘xterm’, ‘xterm-color’, ‘rxvt’, ‘Eterm’, ‘putty’):
sys.ps1 = ’0133[0;32m02>>> 0133[0m02′
sys.ps2 = ’0133[0;32m02… 0133[0m02′
BTW I’d appreciate it if your posts on the planet were complete and not truncated.
> 3. set up a coloured prompt
Hi. I don not like colored prompts very much, but thank you for your suggestion! I’m always happy to learn something new.
> BTW I’d appreciate it if your posts on the planet were complete and not truncated.
Oops, sorry! I didn’t know they were truncated.
I should have fixed the problem now — thanks for your report.
As written, you need to touch the history file before starting the python shell for the first time after setting PYTHONSTARTUP.
Otherwise it complains that the file doesn’t exist.