I am using anaconda on mac and I am wondering whether there is a way to clear my python command history. Specifically, upon calling python
in terminal, I wanted to clear anything I typed before here.

- 825
- 1
- 10
- 22
-
Are you asking how to clear the display of previously entered commands and output, or how to clear the command history that Python goes through when you hit the up arrow key? – user2357112 Nov 29 '18 at 00:23
7 Answers
Assuming you want to clear the command history Python goes through when you hit the up and down arrow keys, that history is managed by either the GNU readline library or libedit, depending on your system. The Python readline
module is the Python-level interface to the underlying library (even if that library is libedit), and on systems where the underlying library supports it, you can clear the history with readline.clear_history
:
>>> import readline
>>> readline.clear_history()
I do not know if the library on your Mac supports it.

- 260,549
- 28
- 431
- 505
-
2This works for removing your password from the python history, when you think you computer is locked, but it wasn't. – peawormsworth Feb 18 '20 at 05:26
-
2Doesn't seem to work.. I still get my command history in the shell – Brendan Metcalfe Jan 02 '21 at 19:11
-
I did that and my history is no longer accessible with the arrow keys, but it seems to still be there - because it still causes an error when opening python. I think my problem was solved by the answer [here](https://stackoverflow.com/a/56124666/2550406) – lucidbrot Feb 11 '21 at 10:27
-
-
Running this doesn't clear the .python_history files in $HOME. This you have to do manually with `rm $HOME/.python_history*` – Scotrraaj Gopal Aug 11 '23 at 16:10
If your system doesn't use readline
, to remove all Python command history just delete the history file at ~/.python_history
.
You can see the full path of the history file with:
python -c "import os; print(os.path.join(os.path.expanduser('~'), '.python_history'))"
.
On Windows the history file is at %userprofile%\.python_history
(e.g., 'C:\Users\<user>\.python_history'
).

- 59
- 1
- 3
If you are on Windows and nothing you found helped you clear the stored command history of your terminal, this is what helped me:
Locate and delete: %userprofile%\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadline\ConsoleHost_history. txt
This will delete your entire terminal history.

- 1,781
- 9
- 17
Escape sequence?
print(chr(27) + "[2J")
Alternatively
import os
os.system('cls' if os.name == 'nt' else 'clear')

- 1,231
- 13
- 25
-
3This will only clear my python history in this interactive session but not the previous ones. Suppose you called python, did some python script, exited, and called python again, when you press up on your keyboard, you should be able to see the python history and that is what I was trying to clear. – John M. Nov 29 '18 at 00:22
You could use subprocess.call() from the standard library subprocess module to send the reset command to the terminal which will reinitialize the terminal and in effect do something similar to clear but also clear the scrollback history.
import subprocess
subprocess.call('reset')

- 3
- 6
-
1But this only clears my bash command history but not my python history? – John M. Nov 29 '18 at 00:21
import os
os.system('cls')
This will only clear python history in this interactive session but not the previous ones.

- 45,075
- 17
- 40
- 72
If you are using a Linux, then:
Import os
os.system('clear')
If you're using Windows:
Import os
os.system('CLS')

- 34,072
- 23
- 111
- 129

- 9
- 1