# 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
Tab-completion is now enabled by default in the interactive interpreter on systems that support readline. History is also enabled by default, and is written to (and read from) the file ~/.python-history.
使用virtual environment时,Python 3也需要这样做。
我使用的版本稍有不同,它为每个虚拟环境保留一个历史文件:
当然可以,只要一个小的启动脚本。来自python教程中的Interactive Input Editing and History Substitution:
从Python 3.4开始,the interactive interpreter supports autocompletion and history out of the box:
使用IPython。
无论如何,您应该这样做,因为它太棒了:持久的命令历史只是许多方法中的一种,它比一般的Python shell要好。
相关问题 更多 >
编程相关推荐