python tab補全模組
安裝readline模組
[root@zabbix-server ~]# pip install readline
編寫補全模組內容
檢視模組內容:
版本一
[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下使用這行內容
# histfile = os.path.join(os.environ[`HOMEPATH`], `.pythonhistory`) # win10下使用這行內容
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 ` ` 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
測試結果(使用tab補全每次都要先匯入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