Persistent Python Command-Line History

前端 未结 5 736
灰色年华
灰色年华 2021-02-04 07:29

I\'d like to be able to \"up-arrow\" to commands that I input in a previous Python interpreter. I have found the readline module which offers functions like:

相关标签:
5条回答
  • 2021-02-04 08:10

    Use PIP to install the pyreadline package:

    pip install pyreadline
    
    0 讨论(0)
  • 2021-02-04 08:16

    Persistent history has been supported out of the box since Python 3.4. See this bug report.

    0 讨论(0)
  • 2021-02-04 08:18

    Try using IPython as a python shell. It already has everything you ask for. They have packages for most popular distros, so install should be very easy.

    0 讨论(0)
  • 2021-02-04 08:22

    If all you want is to use interactive history substitution without all the file stuff, all you need to do is import readline:

    import readline
    

    And then you can use the up/down keys to navigate past commands. Same for 2 or 3.

    This wasn't clear to me from the docs, but maybe I missed it.

    0 讨论(0)
  • 2021-02-04 08:25

    I think the suggestions in the Python documentation pretty much cover what you want. Look at the example pystartup file toward the end of section 13.3:

    • http://docs.python.org/tutorial/interactive.html

    or see this page:

    • http://rc98.net/pystartup

    But, for an out of the box interactive shell that provides all this and more, take a look at using IPython:

    • http://ipython.scipy.org/moin/
    0 讨论(0)
提交回复
热议问题