Python-2.7 配置 tab 自動補全功能

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

 以前一直使用shell編程,習慣了shell的 tab 自動補全功能,而Python的命令行卻不支持 tab 自動補全,故而研究讓Python命令行支持自動補全功能。linux

一、首先找到Python調用庫路徑,具體方法以下:web

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']

二、複製tab.py到Python調用庫路徑:/usr/local/python27/lib/python2.7shell

 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

三、修改系統變量編程

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

四、查看效果api

[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.
相關文章
相關標籤/搜索