python的tab自动补全

之前一直使用shell编程,习惯了shell的 tab 自动补全功能,而Python的命令行却不支持 tab 自动补全,故而研究让Python命令行支持自动补全功能。

1、首先找到Python调用库路径,具体方法如下:

按 Ctrl+C 复制代码

>>>python27
>>>import sys
>>>sys.path
['', '/usr/local/python27/lib/python27.zip', '/usr/local/python27/lib/python2.7', '/usr/local/python27/lib/python2.7/plat-linux2', '/usr/local/python27/lib/python2.7/lib-tk', '/usr/local/python27/lib/python2.7/lib-old', '/usr/local/python27/lib/python2.7/lib-dynload', '/usr/local/python27/lib/python2.7/site-packages']

 

2、复制tab.py到Python调用库路径:/usr/local/python27/lib/python2.7

复制代码
 1 #cat tab.py
 2 #!/usr/bin/env python
 3 # python startup file
 4 import sys
 5 import readline
 6 import rlcompleter
 7 import atexit
 8 import os
 9 # tab completion
10 readline.parse_and_bind('tab: complete')
11 # history file
12 histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
13 try:
14     readline.read_history_file(histfile)
15 except IOError:
16     pass
17 
18 atexit.register(readline.write_history_file, histfile)
19 del os, histfile, readline, rlcompleter
复制代码

3、修改系统变量

#echo "export PYTHONTAB=/usr/local/python27/lib/python2.7/tab.py" >>/etc/profile
#source /etc/profile

4、查看效果

复制代码
[root@web01 python2.7]# python27
Python 2.7.10 (default, Jan 12 2016, 16:23:29) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab
>>> import sys
>>> sys.
sys.__class__(              sys.argv                    sys.hexversion
sys.__delattr__(            sys.builtin_module_names    sys.long_info
sys.__dict__                sys.byteorder               sys.maxint
sys.__displayhook__(        sys.call_tracing(           sys.maxsize
sys.__doc__                 sys.callstats(              sys.maxunicode
sys.__excepthook__(         sys.copyright               sys.meta_path
sys.__format__(             sys.displayhook(            sys.modules
sys.__getattribute__(       sys.dont_write_bytecode     sys.path
sys.__hash__(               sys.exc_clear(              sys.path_hooks
sys.__init__(               sys.exc_info(               sys.path_importer_cache
sys.__name__                sys.exc_type                sys.platform
sys.__new__(                sys.excepthook(             sys.prefix
sys.__package__             sys.exec_prefix             sys.ps1
sys.__reduce__(             sys.executable              sys.ps2
sys.__reduce_ex__(          sys.exit(                   sys.py3kwarning
sys.__repr__(               sys.exitfunc(               sys.setcheckinterval(
sys.__setattr__(            sys.flags                   sys.setdlopenflags(
sys.__sizeof__(             sys.float_info              sys.setprofile(
sys.__stderr__              sys.float_repr_style        sys.setrecursionlimit(
sys.__stdin__               sys.getcheckinterval(       sys.settrace(
sys.__stdout__              sys.getdefaultencoding(     sys.stderr
sys.__str__(                sys.getdlopenflags(         sys.stdin
sys.__subclasshook__(       sys.getfilesystemencoding(  sys.stdout
sys._clear_type_cache(      sys.getprofile(             sys.subversion
sys._current_frames(        sys.getrecursionlimit(      sys.version
sys._getframe(              sys.getrefcount(            sys.version_info
sys._mercurial              sys.getsizeof(              sys.warnoptions
sys.api_version             sys.gettrace(               
>>> sys.
复制代码
posted @ 2017-03-24 08:17  贺秋雨  阅读(174)  评论(0编辑  收藏  举报