Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.
Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same command several times. I'm wondering if, and how, to clear the Python interpreter console.
I've heard about doing a system call and either calling cls
on Windows or clear
on Linux, but I was hoping there was something I could command the interpreter itself to do.
Note: I'm running on Windows, so Ctrl+L doesn't work.
Here's the definitive solution that merges all other answers. Features:
You can use it as you like:
You can import it as a module:
You can call it as a script:
It is truly multiplatform; if it can't recognize your system
(
ce
,nt
,dos
orposix
) it will fall back to printing blank lines.You can download the [full] file here: https://gist.github.com/3130325
Or if you are just looking for the code:
I'm using MINGW/BASH on Windows XP, SP3.
(stick this in .pythonstartup)
# My ctrl-l already kind of worked, but this might help someone else
# leaves prompt at bottom of the window though...
import readline
readline.parse_and_bind('\C-l: clear-screen')
# This works in BASH because I have it in .inputrc as well, but for some
# reason it gets dropped when I go into Python
readline.parse_and_bind('\C-y: kill-whole-line')
I couldn't stand typing 'exit()' anymore and was delighted with martineau's/Triptych's tricks:
I slightly doctored it though (stuck it in .pythonstartup)
EDIT: I've just read "windows", this is for linux users, sorry.
In bash:
Save it as "whatyouwant.sh", chmod +x it then run:
or something other than python (idle, whatever). This will ask you if you actually want to exit, if not it rerun python (or the command you gave as parameter).
This will clear all, the screen and all the variables/object/anything you created/imported in python.
In python just type exit() when you want to exit.
This is the simplest thing you can do and it doesn't require any additional libraries. It will clear the screen and return
>>>
to the top left corner.Magic strings are mentioned above - I believe they come from the terminfo database:
http://www.google.com/?q=x#q=terminfo
http://www.google.com/?q=x#q=tput+command+in+unix
$ tput clear| od -t x1z 0000000 1b 5b 48 1b 5b 32 4a >.[H.[2J< 0000007
Although this is an older question, I thought I'd contribute something summing up what I think were the best of the other answers and add a wrinkle of my own by suggesting that you put these command(s) into a file and set your PYTHONSTARTUP environment variable to point to it. Since I'm on Windows at the moment, it's slightly biased that way, but could easily be slanted some other direction.
Here's some articles I found that describe how to set environment variables on Windows:
When to use sys.path.append and when modifying %PYTHONPATH% is enough
How To Manage Environment Variables in Windows XP
Configuring System and User Environment Variables
How to Use Global System Environment Variables in Windows
BTW, don't put quotes around the path to the file even if it has spaces in it.
Anyway, here's my take on the code to put in (or add to your existing) Python startup script:
BTW, you can also use @Triptych's
__repr__
trick to changeexit()
into justexit
(and ditto for its aliasquit
):Lastly, here's something else that changes the primary interpreter prompt from
>>>
to cwd+>>>
: