首頁 > 軟體

Python中tab鍵自動補全功能的設定

2020-06-16 17:39:14

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

  • 1.首先我們需要檢視python的安裝路徑
[root@localhost ~]# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib64/python26.zip', '/usr/lib64/python2.6', '/usr/lib64/python2.6/plat-linux2', '/usr/lib64/python2.6/lib-tk', '/usr/lib64/python2.6/lib-old', '/usr/lib64/python2.6/lib-dynload', '/usr/lib64/python2.6/site-packages', '/usr/lib64/python2.6/site-packages/gst-0.10', '/usr/lib64/python2.6/site-packages/gtk-2.0', '/usr/lib64/python2.6/site-packages/webkit-1.0', '/usr/lib/python2.6/site-packages']
  • 切換目錄到python的安裝目錄下,進行tab鍵補全模組的編寫。
[root@localhost ~]# cd /usr/lib64/python2.6
[root@localhost python2.6]# vim 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
  • 此處我們編寫完成後就可以進入python中匯入tab鍵補全
root@localhost python2.6]# python
Python 2.6.6 (r266:84292, Oct 12 2012, 14:23:48) 
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab
>>> sys.
sys.__class__(              sys.__reduce_ex__(          sys.builtin_module_names    sys.exitfunc(               sys.maxsize                 sys.setprofile(
sys.__delattr__(            sys.__repr__(               sys.byteorder               sys.flags                   sys.maxunicode              sys.setrecursionlimit(
sys.__dict__                sys.__setattr__(            sys.call_tracing(           sys.float_info              sys.meta_path               sys.settrace(
sys.__displayhook__(        sys.__sizeof__(             sys.callstats(              sys.getcheckinterval(       sys.modules                 sys.stderr
sys.__doc__                 sys.__stderr__              sys.copyright               sys.getdefaultencoding(     sys.path                    sys.stdin
sys.__excepthook__(         sys.__stdin__               sys.displayhook(            sys.getdlopenflags(         sys.path_hooks              sys.stdout
sys.__format__(             sys.__stdout__              sys.dont_write_bytecode     sys.getfilesystemencoding(  sys.path_importer_cache     sys.subversion
sys.__getattribute__(       sys.__str__(                sys.exc_clear(              sys.getprofile(             sys.platform                sys.version
sys.__hash__(               sys.__subclasshook__(       sys.exc_info(               sys.getrecursionlimit(      sys.prefix                  sys.version_info
sys.__init__(               sys._clear_type_cache(      sys.exc_type                sys.getrefcount(            sys.ps1                     sys.warnoptions
sys.__name__                sys._current_frames(        sys.excepthook(             sys.getsizeof(              sys.ps2                     
sys.__new__(                sys._getframe(              sys.exec_prefix             sys.gettrace(               sys.py3kwarning             
sys.__package__             sys.api_version             sys.executable              sys.hexversion              sys.setcheckinterval(       
sys.__reduce__(             sys.argv                    sys.exit(                   sys.maxint                  sys.setdlopenflags(         
  • 為了進入python之後不用每次都匯入,來進行下邊的操作,進入家目錄,編寫 .bashrc檔案在最後新增下面一行,儲存退出。
[root@localhost ~]# cd ~
[root@localhost ~]# vim .bashrc

export PYTHONSTARTUP=/usr/lib64/python2.6/tab.py
  • 家目錄.bashrc檔案只有使用者登入時才會載入生效,需要進行下面的操作來生效
[root@localhost ~]# source .bashrc 

OK,那麼現在我們就可以不用每次進行python之後進行import tab 動作來使tab鍵補全功能生效了。

CentOS上原始碼安裝Python3.4  http://www.linuxidc.com/Linux/2015-01/111870.htm

《Python核心程式設計 第二版》.(Wesley J. Chun ).[高清PDF中文版] http://www.linuxidc.com/Linux/2013-06/85425.htm

《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視訊+程式碼] http://www.linuxidc.com/Linux/2013-11/92693.htm

Python指令碼獲取Linux系統資訊 http://www.linuxidc.com/Linux/2013-08/88531.htm

Ubuntu下用Python搭建桌面演算法交易研究環境 http://www.linuxidc.com/Linux/2013-11/92534.htm

Python 語言的發展簡史 http://www.linuxidc.com/Linux/2014-09/107206.htm


IT145.com E-mail:sddin#qq.com