歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Python 添加tab補全

在平時查看Python方法用到tab補全還是很方便的。

  • 1. mac 平台 配置如下:
      •   mac是類Unix平台,需要在添加一條配置內容到bash_profile 中(默認是沒有這個文件,可以新建一個放在宿主目錄下面)
        •   先新建一個tab.py的文件內容如下:
        • import rlcompleter,sys,readline
           
          if sys.platform == 'darwin' and sys.version_info[0] == 2:
              readline.parse_and_bind("bind ^I rl_complete")
          else:
              readline.parse_and_bind("tab: complete")

      •  查看當前系統的Python解釋器路徑
        Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
        [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
        Type "help", "copyright", "credits" or "license" for more information.
        >>> import sys
        >>> sys.path
        ['', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/beautifulsoup4-4.4.1-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/requests-2.9.0-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/testkit_lite-3.1.14-py2.7.egg', '/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python27.zip', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-darwin', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-old', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-dynload', '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages', '/Library/Python/2.7/site-packages']
      • 將剛剛新建好的tab.py 文件復制到解釋器路徑下面,一般放置在‘/Library/Python/2.7/site-packages/’目錄下面
        •   sudo cp tab.py /Library/Python/2.7/site/packages/
      • 將配置文件的信息寫入到 ~/.bash_profile 中,如果文件存在則添加在文件內容後,如果不存在則新建該文件
        • vi ~/.bash_profile    保存退出然後 source ~/.bash_profile   (讓該配置文件生效)
        • export PYTHONSTARTUP=/Library/Python/2.7/site-packages/tab.py

        然後進入Python import tab 用tab則可以列出子方法如下

Python 2.7.6 (v2.7.6:3a1db0d2747e, Nov 10 2013, 00:42:54) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab,sys
>>> sys.
Display all 101 possibilities? (y or n) 
sys.__class__(             sys.__hash__(              sys.__sizeof__(            sys.callstats(             sys.getfilesystemencoding( sys.prefix                
sys.__class__(             sys.__init__(              sys.__stderr__             sys.copyright              sys.getprofile(            sys.ps1                   
sys.__class__(             sys.__init__(              sys.__stdin__              sys.displayhook(           sys.getrecursionlimit(     sys.ps2                   
sys.__delattr__(           sys.__name__               sys.__stdout__             sys.dont_write_bytecode    sys.getrefcount(           sys.py3kwarning           
sys.__delattr__(           sys.__new__(               sys.__str__(               sys.exc_clear(             sys.getsizeof(             sys.setcheckinterval(     
sys.__dict__               sys.__new__(               sys.__str__(               sys.exc_info(              sys.gettrace(              sys.setdlopenflags(       
sys.__displayhook__(       sys.__package__            sys.__subclasshook__(      sys.exc_type               sys.hexversion             sys.setprofile(           
sys.__doc__                sys.__plen                 sys.__subclasshook__(      sys.excepthook(            sys.long_info              sys.setrecursionlimit(    
sys.__doc__                sys.__reduce__(            sys._clear_type_cache(     sys.exec_prefix            sys.maxint                 sys.settrace(             
sys.__doc__                sys.__reduce__(            sys._current_frames(       sys.executable             sys.maxsize                sys.stderr                
sys.__egginsert            sys.__reduce_ex__(         sys._getframe(             sys.exit(                  sys.maxunicode             sys.stdin                 
sys.__excepthook__(        sys.__reduce_ex__(         sys._mercurial             sys.flags                  sys.meta_path              sys.stdout                
sys.__format__(            sys.__repr__(              sys.api_version            sys.float_info             sys.modules                sys.subversion            
sys.__format__(            sys.__repr__(              sys.argv                   sys.float_repr_style       sys.path                   sys.version               
sys.__getattribute__(      sys.__setattr__(           sys.builtin_module_names   sys.getcheckinterval(      sys.path_hooks             sys.version_info          
sys.__getattribute__(      sys.__setattr__(           sys.byteorder              sys.getdefaultencoding(    sys.path_importer_cache    sys.warnoptions           
sys.__hash__(              sys.__sizeof__(            sys.call_tracing(          sys.getdlopenflags(        sys.platform              
>>> sys. 

2.Ubuntu系統的配置文件和Mac有點不一樣,同樣寫入一個tab.py文件到Python解釋器路徑下,其他的操作都不用,添加的代碼如下:

sudo vi /usr/lib/python2.7/dist-packages/tab.py

import sys,readline,rlcompleter,atexit,os

readline.parse_and_bind('tab: complete')
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

Copyright © Linux教程網 All Rights Reserved