先创建文件 tab.py

vim tab.py

#!/usr/bin/env python

# python startup file

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

 

进入python查看路径

>>> import sys

>>> sys.path

['', '/usr/lib/python2.7','/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk','/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload','/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages','/usr/lib/python2.7/dist-packages/PILcompat','/usr/lib/python2.7/dist-packages/gtk-2.0','/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

 (路径有可能不一样,主要找类似的目录然后试验一下)

一般我们会将这一类代码放在/usr/local/lib/python2.7/dist-packages目录下

mv tab.py/usr/local/lib/python2.7/dist-packages

 

测试

>>> import tab

>>>import sys

>>> sys.(按tab键测试是否能够弹出)

可以在脚本中写入import tab,这样运行脚本的时候,输入参数是,就可以删除或者按方向键上调用历史命令等。

代码来自于网络,向原作者致谢。

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐