How do you see the entire command history in interactive Python?

后端 未结 10 1750
不知归路
不知归路 2020-11-30 16:36

I\'m working on the default python interpreter on Mac OS X, and I Cmd+K (cleared) my earlier commands. I can go through them one by one using the arrow

相关标签:
10条回答
  • 2020-11-30 17:00

    With python 3 interpreter the history is written to
    ~/.python_history

    0 讨论(0)
  • 2020-11-30 17:04

    A simple function to get the history similar to unix/bash version.

    Hope it helps some new folks.

    def ipyhistory(lastn=None):
        """
        param: lastn Defaults to None i.e 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 it helps fellas.

    0 讨论(0)
  • 2020-11-30 17:09

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

    0 讨论(0)
  • 2020-11-30 17:09

    If you want to write the history to a file:

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

    The 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.
    
    0 讨论(0)
  • 2020-11-30 17:11

    In IPython %history -g should give you the entire command history. The default configuration also saves your history into a file named .python_history in your user directory.

    0 讨论(0)
  • 2020-11-30 17:13

    Code for printing the entire history:

    Python 3

    One-liner (quick copy and paste):

    import readline; print('\n'.join([str(readline.get_history_item(i + 1)) for i in range(readline.get_current_history_length())]))
    

    (Or longer version...)

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

    Python 2

    One-liner (quick copy and paste):

    import readline; print '\n'.join([str(readline.get_history_item(i + 1)) for i in range(readline.get_current_history_length())])
    

    (Or longer version...)

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

    Note: get_history_item() is indexed from 1 to n.

    0 讨论(0)
提交回复
热议问题