Python-2.7 配置 tab 自动补全功能

作者博文地址:http://www.cnblogs.com/liu-shuai/ 

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

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

1 >>>python27
2 >>>import sys
3 >>>sys.path
4 ['', '/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.
上一篇:阿里云服务器双十一冰点底价:86元/年起,现在可以购买啦!


下一篇:ADT开发中的一些优化设置:代码背景色、代码字体大小、代码自动补全