Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear python console command history

Tags:

python

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.

like image 286
John M. Avatar asked Nov 29 '18 00:11

John M.


People also ask

How do I clear the Python console log?

In an interactive shell/terminal, we can simply use ctrl+l to clear the screen.

How do you clear Python history?

The commands used to clear the terminal or Python shell are cls and clear.

How do I clear all idle in Python?

Type python and hit enter to turn windows command prompt to python idle (make sure python is installed). Type quit() and hit enter to turn it back to windows command prompt. Type cls and hit enter to clear the command prompt/ windows shell.


1 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.

like image 91
user2357112 supports Monica Avatar answered Oct 05 '22 23:10

user2357112 supports Monica