python交互模式下Tab键自动补全

in Python with 0 comment

1.新建如下脚本:

# cat ~/.pythonstartup

#!/usr/bin/env python
import sys
import readline
import rlcompleter
import atexit
import os
# tab completion
readline.parse_and_bind('tab: complete')
# history file
histfile = os.path.join(os.environ['HOME'], '.pythonstartup')
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile) 
del os, histfile, readline, rlcompleter

2.配置环境变量

echo 'export PYTHONSTARTUP=/root/.pythonstartup'>> /etc/profile

3.重新登陆shell,输入python命令进入交互模式,就可以用Tab键进行补全。

Responses