简体   繁体   English

如何在 ptpython 控制台中读取历史记录?

[英]How to read history in ptpython console?

I've been trying to figure out how to get save and read in the history of my Python commands in a ptpython console, but haven't been able to do so.我一直在试图弄清楚如何在ptpython控制台中保存和读取我的 Python 命令的历史记录,但一直没能做到。 All of my efforts have so far been variations of this answer .到目前为止,我所有的努力都是这个答案的变体。 However, I still am not able to read in my history.但是,我仍然无法阅读我的历史。

I would simply like to be able to press the and arrows to go through my Python commands from a previous console session ( not the current console session that I'm in).我只是希望能够按箭头从以前的控制台会话(不是我所在的当前控制台会话)中浏览我的 Python 命令。 Here's what I currently have in my $PYTHONSTARTUP file:这是我目前在$PYTHONSTARTUP文件中的内容:

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it:  "export PYTHONSTARTUP=/home/user/.pystartup" in bash.
#
# Note that PYTHONSTARTUP does *not* expand "~", so you have to put in the
# full path to your home directory.

import atexit
import os
import readline
import rlcompleter
import sys
try:
    from ptpython.repl import embed
except ImportError:
    print('ptpython is not available: falling back to standard prompt')
else:
    sys.exit(embed(globals(), locals()))

historyPath = os.path.expanduser("~/.ptpython/history")

def save_history(historyPath=historyPath):
   import readline
   readline.write_history_file(historyPath)

if os.path.exists(historyPath):
   readline.read_history_file(historyPath)

atexit.register(save_history)
readline.parse_and_bind('tab: complete')
del os, atexit, readline, rlcompleter, save_history, historyPath

And my $PYTHONSTARTUP variable is:我的$PYTHONSTARTUP变量是:

$ echo $PYTHONSTARTUP 
/Users/[redacted]/.pystartup

I'm on Python 3.7.3, macOS 10.14.6, and ptpython 2.0.4.我使用的是 Python 3.7.3、macOS 10.14.6 和 ptpython 2.0.4。

Thanks谢谢

If you check source code for embed then you see option history_filename=如果您检查嵌入的源代码,那么您会看到选项history_filename=

embed(globals(), locals(), history_filename=historyPath)

import os

try:
    from ptpython.repl import embed
except ImportError:
    print('ptpython is not available: falling back to standard prompt')
else:
    history_path = os.path.expanduser("~/.ptpython/history")
    embed(globals(), locals(), history_filename=history_path)

BTW: If folder ~/.ptpython doesn't exists then you will have to create it before run code.顺便说一句:如果文件夹~/.ptpython不存在,那么您必须在运行代码之前创建它。

EDIT (2022):编辑(2022):

import os

try:
    from ptpython.repl import embed
except ImportError:
    print('ptpython is not available: falling back to standard prompt')
else:
    history_dir  = os.path.expanduser("~/.ptpython")
    history_path = os.path.join(history_dir, "history")
    
    if not os.path.exists(history_path):
        os.makedirs(history_dir, exist_ok=True)  # create folder if not exist
        open(history_path, 'a').close()          # create empty file
        
    embed(globals(), locals(), history_filename=history_path)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM