python tab补全_python-tab补全模块

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值