python命令行添加自动补全和命令历史功能

# python startup file
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
# win histfile = os.path.join(os.environ['HOMEPATH'], '.pythonhistory')
# linux
histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
try: readline.read_history_file(histfile) except IOError: pass atexit.register(readline.write_history_file, histfile) del os, histfile, readline, rlcompleter

将以上文件保存为.pythonstartup并存放在用户的主目录下,window放在C:\Users\Administrator下,linux放在/root或者/home/xxx目录下,然后设置环境变量

linux中export PYTHONSTARTUP=~/.pythonstartup

windows中一样,打开控制面板\系统和安全\系统,高级系统设置,环境变量,新建系统变量,变量名为PYTHONSTARTUP,变量值为之前的文件

python3中需要安装pyreadline,pip install pyreadline

 linux和window中需要修改下HOME,HOMEPATH

posted @ 2018-03-07 11:41  菲菲菲菲菲常新的新手  阅读(2331)  评论(0编辑  收藏  举报