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 '\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
测试结果(使用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
网友评论