python tab補全模塊
安裝readline模塊
[root@zabbix-server ~]# pip install readline
編寫補全模塊內(nèi)容
查看模塊內(nèi)容:
版本一
[root@zabbix-server ~]# cat tab.py
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# __created by junxi__
# The script is used by python tab Completion script
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') # linux下使用這行內(nèi)容
# histfile = os.path.join(os.environ['HOMEPATH'], '.pythonhistory') # win10下使用這行內(nèi)容
try:
readline.read_history_file(histfile)
except IOError:
pass
# atexit.register(readline.write_history_file, histfile)
# del histfile
版本二
[root@zabbix-server ~]# cat tab.py
#!/usr/bin/env python
# _*_ coding:utf-8 _*_
# __created by junxi__
# The script is used by python tab Completion script
import readline,rlcompleter
### Indenting
class TabCompleter(rlcompleter.Completer):
"""Completer that supports indenting"""
def complete(self, text, state):
if not text:
return (' ', None)[state]
else:
return rlcompleter.Completer.complete(self, text, state)
readline.set_completer(TabCompleter().complete)
### Add autocompletion
if 'libedit' in readline.__doc__:
readline.parse_and_bind("bind -e")
readline.parse_and_bind("bind '\t' rl_complete")
else:
readline.parse_and_bind("tab: complete")
### Add history
import os
histfile = os.path.join(os.environ["HOME"], ".pyhist")
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
del histfile
查看python默認可以查找到的包目錄(就是默認可以import 模塊的路徑)
[root@zabbix-server ~]# python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python2.7/site-packages/pip-9.0.1-py2.7.egg', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2', '/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload', '/usr/lib64/python2.7/site-packages', '/usr/lib64/python2.7/site-packages/gtk-2.0', '/usr/lib/python2.7/site-packages']
把tab.py移動到sys.path查看的目錄中的一個即可
[root@zabbix-server ~]# mv tab.py /usr/lib/python2.7/site-packages
測試結(jié)果(使用tab補全每次都要先導(dǎo)入tab.py補全模塊)
[root@zabbix-server ~]# python
Python 2.7.5 (default, Nov 6 2016, 00:28:07)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab
>>> import os
>>> os.
Display all 249 possibilities? (y or n)
os.EX_CANTCREAT os.__package__ os.listdir(
os.EX_CONFIG os.__reduce__( os.lseek(
os.EX_DATAERR os.__reduce_ex__( os.lstat(
os.EX_IOERR os.__repr__( os.major(
os.EX_NOHOST os.__setattr__( os.makedev(
os.EX_NOINPUT os.__sizeof__( os.makedirs(
os.EX_NOPERM os.__str__( os.minor(
ok