How do you see the whole history of commands in interactive python?

I am working on the default python interpreter on Mac OS X, and I Cmd + K (cleared) my previous commands. I can go through them one by one using the arrow keys. But is there a -history option in the bash shell that shows you all the commands you have entered so far?

+124
python macos
Jul 02 '11 at 18:15
source share
9 answers

Use readline.get_current_history_length() to get the length, and readline.get_history_item() to view each one.

+57
Jul 02 '11 at 18:28
source share

Code for printing the whole story (for future reference only):

python2

 import readline for i in range(readline.get_current_history_length()): print readline.get_history_item(i + 1) 

python3

 import readline for i in range(readline.get_current_history_length()): print (readline.get_history_item(i + 1)) 

Edit : note get_history_item() indexed from 1 to n.

+212
Aug 10 2018-11-11T00:
source share

With python 3 interpreter, history is written in
~/.python_history

+33
Aug 21 '16 at 11:55
source share

Since the above only works for python 2.x, for python 3.x (in particular 3.5) is similar, but with a slight modification:

 import readline for i in range(readline.get_current_history_length()): print (readline.get_history_item(i + 1)) 

pay attention to extra ()

(using shell scripts to parse .python_history or using python to modify the above code is a matter of personal taste and imho situation)

+4
May 16 '17 at 22:18
source share

If you want to write history to a file:

 import readline readline.write_history_file('python_history.txt') 

Help function gives:

 Help on built-in function write_history_file in module readline: write_history_file(...) write_history_file([filename]) -> None Save a readline history file. The default filename is ~/.history. 
+3
Dec 01 '17 at 14:22
source share

A simple function to get a history similar to the Unix / Bash version.

Hope this helps some new people.

 def ipyhistory(lastn=None): """ param: lastn Defaults to None ie full history. If specified then returns lastn records from history. Also takes -ve sequence for first n history records. """ import readline assert lastn is None or isinstance(lastn, int), "Only integers are allowed." hlen = readline.get_current_history_length() is_neg = lastn is not None and lastn < 0 if not is_neg: flen = len(str(hlen)) if not lastn else len(str(lastn)) for r in range(1,hlen+1) if not lastn else range(1, hlen+1)[-lastn:]: print(": ".join([str(r if not lastn else r + lastn - hlen ).rjust(flen), readline.get_history_item(r)])) else: flen = len(str(-hlen)) for r in range(1, -lastn + 1): print(": ".join([str(r).rjust(flen), readline.get_history_item(r)])) 

Snippet: tested with Python3. Let me know if there are any glitches with python2. Samples:

Full History: ipyhistory()

Last 10 History: ipyhistory(10)

First 10 History: ipyhistory(-10)

Hope this helps the guys.

+2
Sep 30 '18 at 7:23
source share

In IPython, %history -g should display the entire history of commands. The default configuration also saves your history to a file named .python_history in your user directory.

+2
Feb 11 '19 at 11:11
source share

This should give you the commands printed on separate lines:

 import readline map(lambda p:print(readline.get_history_item(p)), map(lambda p:p, range(readline.get_current_history_length())) ) 
+1
Oct 17 '17 at 16:05
source share

@Jason-B, it really helps, thanks. then I found this example and compiled for my own fragment.

 #!/usr/bin/env python3 import os, readline, atexit python_history = os.path.join(os.environ['HOME'], '.python_history') try: readline.read_history_file(python_history) readline.parse_and_bind("tab: complete") readline.set_history_length(5000) atexit.register(readline.write_history_file, python_history) except IOError: pass del os, python_history, readline, atexit 
+1
Nov 20 '17 at 14:59
source share



All Articles