Changing Python's prompt
May. 2nd, 2014 12:51 pmI have a mild dislike of Python's default prompt, ">>>". Not that the prompt itself is bad, but when you copy from an interactive session and paste into email or a Usenet post, the prompt clashes with the standard > email quote marker. So I've changed my first level prompt to "py>" to distinguish interactive sessions from email quotes. Doing so is very simple:
Note the space at the end.
You can change the second level prompt (by default, "...") by assigning to
If you're trying to use coloured prompts, there are some subtitles to be aware of. You have to escape any non-printing characters. See this bug report for details.
You can have Python automatically use your custom prompt by setting it your startup file. If the environment variable
and the startup file itself then sets the prompt, as shown above.
import sys sys.ps1 = 'py> '
Note the space at the end.
You can change the second level prompt (by default, "...") by assigning to
sys.ps2
, but I haven't bothered. Both prompts support arbitrary objects, not just strings, which you can use to implement dynamic prompts similar to those iPython uses. Here's a simple example of numbering the prompts:class Prompt: def __init__(self): self.count = 0 def __str__(self): self.count += 1 return "[%4d] " % self.count sys.ps1 = Prompt()
If you're trying to use coloured prompts, there are some subtitles to be aware of. You have to escape any non-printing characters. See this bug report for details.
You can have Python automatically use your custom prompt by setting it your startup file. If the environment variable
PYTHONSTARTUPFILE
is set, Python will run the file named in that environment variable when you start the interactive interpreter. As I am using Linux for my desktop, I have the following line in my .bashrc
file to set the environment variable each time I log in:export PYTHONSTARTUP=/home/steve/python/startup.py
and the startup file itself then sets the prompt, as shown above.