python中的tab补全功能添加

时间:2023-03-08 17:17:34

用Python时没有tab补全还是挺痛苦的,记录一下添加该功能的方法利人利己

1. 先准备一个tab.py的脚本

shell> cat 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

2. 查看Python默认的模块存放地址

[root@localhost ~]# python
Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib/python2.6/site-packages', '/usr/lib/python2.6/site-packages/setuptools-0.6c11-py2.6.egg-info']

3. 拷贝该脚本到默认模块存放路径

cp tab.py  /usr/lib64/python2.6/

4. 现在可以用了

>>> import tab
>>> import sys
>>> sys.
sys.__class__( sys.__reduce_ex__( sys.builtin_module_names sys.exitfunc( sys.last_type( sys.py3kwarning
sys.__delattr__( sys.__repr__( sys.byteorder sys.flags sys.last_value sys.setcheckinterval(
sys.__dict__ sys.__setattr__( sys.call_tracing( sys.float_info sys.maxint sys.setdlopenflags(
sys.__displayhook__( sys.__sizeof__( sys.callstats( sys.getcheckinterval( sys.maxsize sys.setprofile(
sys.__doc__ sys.__stderr__ sys.copyright sys.getdefaultencoding( sys.maxunicode sys.setrecursionlimit(
sys.__excepthook__( sys.__stdin__ sys.displayhook( sys.getdlopenflags( sys.meta_path sys.settrace(
sys.__format__( sys.__stdout__ sys.dont_write_bytecode sys.getfilesystemencoding( sys.modules sys.stderr
sys.__getattribute__( sys.__str__( sys.exc_clear( sys.getprofile( sys.path sys.stdin
sys.__hash__( sys.__subclasshook__( sys.exc_info( sys.getrecursionlimit( sys.path_hooks sys.stdout
sys.__init__( sys._clear_type_cache( sys.exc_type sys.getrefcount( sys.path_importer_cache sys.subversion
sys.__name__ sys._current_frames( sys.excepthook( sys.getsizeof( sys.platform sys.version
sys.__new__( sys._getframe( sys.exec_prefix sys.gettrace( sys.prefix sys.version_info
sys.__package__ sys.api_version sys.executable sys.hexversion sys.ps1 sys.warnoptions
sys.__reduce__( sys.argv sys.exit( sys.last_traceback sys.ps2