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

作者博文地址:http://www.cnblogs.com/spiritman/ 

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

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

1 >>>python27
2 >>>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

 #cat tab.py
#!/usr/bin/env python
# python startup 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

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.

作者博文地址:http://www.cnblogs.com/spiritman/

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


下一篇:JavaScript 参考手册——javascript本地和内置对象、BOM、DOM