1. 程式人生 > >python添加tab鍵功能

python添加tab鍵功能

ror spl edi trac interval deb att name bytecode

學習Python的時候,如何沒有tab鍵補全功能,我感覺那將是一個噩夢,對於我們這種菜鳥來說,剛接觸python,對一切都不了解,還好有前輩們的指導,學習一下,並記錄下來,還沒有學習這個功能小夥伴們!

環境:

[root@localhost ~]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
[root@localhost ~]#

具體過程:

[root@localhost site-packages]# python #進入python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys #導入sys模塊


>>> sys.path #確認系統默認擼覺
[‘‘, ‘/usr/lib64/python27.zip‘, ‘/usr/lib64/python2.7‘, ‘/usr/lib64/python2.7/plat-linux2‘, ‘/usr/lib64/python2.7/lib-tk‘, ‘/usr/lib64/python2.7/lib-old‘, ‘/usr/lib64/python2.7/lib-dynload‘, ‘/usr/lib64/python2.7/site-packages‘, ‘/usr/lib/python2.7/site-packages‘]
>>> exit()
[root@localhost site-packages]# pwd #進入要放入tab.py文件的目錄(上面的路徑,隨便放一個),我防止如下目錄:

/usr/lib64/python2.7/site-packages

###########################################################

[root@localhost site-packages]# 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

###########################################################

[root@localhost site-packages]# python
Python 2.7.5 (default, Aug 4 2017, 00:39:18)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab #使用前先導入tab模塊
>>> import sys #下面就可以使用tab鍵了
>>> sys.
sys.__class__( sys.__sizeof__( sys.copyright sys.getfilesystemencoding( sys.prefix
sys.__delattr__( sys.__stderr__ sys.displayhook( sys.getprofile( sys.ps1
sys.__dict__ sys.__stdin__ sys.dont_write_bytecode sys.getrecursionlimit( sys.ps2
sys.__displayhook__( sys.__stdout__ sys.exc_clear( sys.getrefcount( sys.py3kwarning
sys.__doc__ sys.__str__( sys.exc_info( sys.getsizeof( sys.pydebug
sys.__excepthook__( sys.__subclasshook__( sys.exc_type sys.gettrace( sys.setcheckinterval(
sys.__format__( sys._clear_type_cache( sys.excepthook( sys.hexversion sys.setdlopenflags(
sys.__getattribute__( sys._current_frames( sys.exec_prefix sys.long_info sys.setprofile(
sys.__hash__( sys._debugmallocstats( sys.executable sys.maxint sys.setrecursionlimit(
sys.__init__( sys._getframe( sys.exit( sys.maxsize sys.settrace(
sys.__name__ sys._mercurial sys.exitfunc( sys.maxunicode sys.stderr
sys.__new__( sys.api_version sys.flags sys.meta_path sys.stdin
sys.__package__ sys.argv sys.float_info sys.modules sys.stdout
sys.__reduce__( sys.builtin_module_names sys.float_repr_style sys.path sys.subversion
sys.__reduce_ex__( sys.byteorder sys.getcheckinterval( sys.path_hooks sys.version
sys.__repr__( sys.call_tracing( sys.getdefaultencoding( sys.path_importer_cache sys.version_info
sys.__setattr__( sys.callstats( sys.getdlopenflags( sys.platform sys.warnoptions
>>> sys.

python添加tab鍵功能