python命令行添加Tab键自动补全

1、编写一个tab的自动补全脚本,名为tab.py

#!/usr/bin/python 
# python tab complete 

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'], '.pythonhistory') 
try: 
    readline.read_history_file(histfile) 
except IOError: 
    pass 
atexit.register(readline.write_history_file, histfile) 
 
del os, histfile, readline, rlcompleter 

2、在python中查看python的模块路径信息

>>> import sys
>>> sys.path
['', '/usr/lib/python26.zip', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/gst-0.10', '/usr/lib/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages/webkit-1.0']
>>> 

python的模块放在了/usr/lib/python26下面,将脚本拷贝到该目录下,在使用时导入即可。

3、导入tab

>>> import tab
>>> os.
Display all 244 possibilities? (y or n)

 4、但python读取模块的路径顺序优先是从当前目录开始,所以若是当前目录也存在tab.py,但内容不同的python脚本,则可能会报错,所以在环境变量中也指定tab.py脚本

#for python
export PYTHONSTARTUP=/usr/lib/python2.6/tab.py

 

posted on 2014-09-27 16:20  Still water run deep  阅读(2676)  评论(0编辑  收藏  举报

导航