python添加tab键自动补全功能

时间:2023-12-24 09:23:31

默认python是没有tab键补全功能的:

>>> import tab
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named tab
>>>

  

创建tab.py文件

# vi tab.py

添加以下内容:

#!/usr/bin/python
# python tab 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默认的模块存放地址

# python
Python 2.7.5 (default, Oct 11 2015, 17:47:16)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/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文件拷贝到/usr/lib64/python2.7/

# cp tab.py  /usr/lib64/python2.7/

  

这样就可以使用tab补全了:

>>> import tab
>>> import sys
>>> sys.
sys.__class__( sys.exitfunc(
sys.__delattr__( sys.flags
sys.__dict__ sys.float_info
sys.__displayhook__( sys.float_repr_style
sys.__doc__ sys.getcheckinterval(
sys.__excepthook__( sys.getdefaultencoding(
sys.__format__( sys.getdlopenflags(
sys.__getattribute__( sys.getfilesystemencoding(
......