给Pythonterminal一个持久的历史

有没有办法告诉交互式Python shell来保存会话之间执行的命令的历史logging?

当一个会话正在运行,命令执行后,我可以向上和访问所述命令,我只是想知道是否有一些方法可以保存一定数量的这些命令,直到下一次我使用Python shell 。

这会非常有用,因为我发现自己在会话中重复使用了命令,而在上次会话结束时使用了这些命令。

当然,你可以用一个小的启动脚本。 从Python教程中的交互式输入编辑和历史替换 :

# 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=~/.pystartup" in bash. import atexit import os import readline import rlcompleter historyPath = os.path.expanduser("~/.pyhistory") 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) del os, atexit, readline, rlcompleter, save_history, historyPath 

从Python 3.4开始, 交互式解释器支持自动完成和开箱即用的历史记录 :

在支持readline系统上,交互式解释器默认启用Tab- readline 。 历史也是默认启用的,并被写入(和读取)文件~/.python-history

使用IPython 。

无论如何,你应该这样做,因为它非常棒:持久的命令历史只是其中许多方法中的一个,比股票Python shell更好。