背景 https://stackoverflow.com/questions/76566358: 这个答案 https://stackoverflow.com/a/47595405/2153235依赖于 readline 模块并且write_history_file
其中的功能。我必须考虑在 Windows 10 上使用 Conda 提示符的差异(这只是为 Conda 设置了环境变量的 CMD 提示符)。为此,c:\User.Name 中没有历史记录文件。另外,我需要pyreadline3 https://stackoverflow.com/a/51964654/2153235.
即使有 100 个命令的空间,命令历史记录中也不会存储任何命令。用于启动 Python、导入 pyreadline3、查询命令历史记录大小并将空命令历史记录写入文件的命令:
REM "CMD" commands in Windows to start Python 3.9
REM environment from Conda prompt
conda activate py39
python
# Load and alias derivative of readline module for Windows.
# From https://stackoverflow.com/a/76566474/2153235
import pyreadline3
readline = pyreadline3.BaseReadline()
# Maximum number of commands storable is 100
readline.get_history_length()
# Sacrifical commands to test the command history
"dog"
'cat'
help(readline)
# Shows get_history_length and get_current_history_length
# Number of stored commands is persistently 0
readline.get_current_history_length()
# Therefore, a zero-length history file is written.
# From https://stackoverflow.com/a/76566474/2153235
readline.write_history_file("./pyHistory.txt")
# Loop prints out zero historical commands.
# From https://stackoverflow.com/questions/6558765, with "list"
# variations at https://stackoverflow.com/questions/44894992
for i in range(readline.get_current_history_length()):
print (readline.get_history_item(i + 1))
如何让它发挥作用?
F7 https://stackoverflow.com/a/63038992/2153235 can bring up a context menu of past commands, but that's far from being able to browse a large history and doesn't allow mashing/bashing to recompose commands. Also doesn't work when I use Cygwin's xterm to invoke the CMD command that launches Conda. I've done this to capture input and output of a session using TypeScript. It's easier to navigate and search the recorded text file than scrolling up the terminal window. Typical terminals have a limited buffer size.
有人建议 Jupyter 不需要历史文件。它似乎与历史文件有不同的用途,并且不记录执行的命令,因此很难重建哪些事件影响了堆栈帧中不同范围内的对象状态以及堆栈帧本身。人们可以查看终端,但缓冲区大小有限,并且大量输出可能会导致很难重建准确的输入。我还发现%USERPROFILE%\.python_history
捕获 REPL 命令,但在退出 Python REPL 会话之前不会更新。
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)