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. (v2.7.6:3a1db0d2747e, Nov  , ::)
        [GCC 4.2. (Apple Inc. build ) (dot )] 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. (v2.7.6:3a1db0d2747e, Nov  , ::)
[GCC 4.2. (Apple Inc. build ) (dot )] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tab,sys
>>> sys.
Display all 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./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

            

上一篇:怎样在一个HTML中嵌入另一个HTML页面(iframe标签用法)


下一篇:vim编辑python脚本时Tab补全