你好,游客 登录 注册 搜索
背景:
阅读新闻

Python 添加tab补全

[日期:2016-12-31] 来源:Linux社区  作者:zhaomx [字体: ]

在平时查看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

本文永久更新链接地址http://www.linuxidc.com/Linux/2016-12/139010.htm

linux
相关资讯       Python tab补全 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款