码迷,mamicode.com
首页 > 编程语言 > 详细

python tab 自动补全

时间:2016-06-26 16:30:39      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:

学习python,经常要使用python命令行查找一些不熟悉的使用方法等等,但是python命令行下没有自带tab补全的功能,看见别人写了tab,可以解决特此记下,以备后用

1.创建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

2.代码有了,我们还需要将脚本放到python指定的目录下,可以使用sys.path来查看一下

>>> import sys
>>> sys.path
[‘‘, /usr/lib/python2.7, /usr/lib/python2.7/plat-x86_64-linux-gnu, /usr/lib/python2.7/lib-tk, /usr/lib/python2.7/lib-old, /usr/lib/python2.7/lib-dynload, /usr/local/lib/python2.7/dist-packages, /usr/lib/python2.7/dist-packages, /usr/lib/python2.7/dist-packages/PILcompat, /usr/lib/python2.7/dist-packages/gtk-2.0, /usr/lib/python2.7/dist-packages/ubuntu-sso-client]

 一般我们会将这一类代码放在/usr/local/lib/python2.7/dist-packages目录下

3.把tab.py 放到指定目录下

mv tab.py /usr/local/lib/python2.7/dist-packages

4.使用

>>> import tab
>>> sys.
sys.__class__(              sys.__subclasshook__(       sys.exitfunc(               sys.path_importer_cache
sys.__delattr__(            sys._clear_type_cache(      sys.flags                   sys.platform
sys.__dict__                sys._current_frames(        sys.float_info              sys.prefix
sys.__displayhook__(        sys._getframe(              sys.float_repr_style        sys.ps1
sys.__doc__                 sys._mercurial              sys.getcheckinterval(       sys.ps2
sys.__excepthook__(         sys._multiarch              sys.getdefaultencoding(     sys.py3kwarning
sys.__format__(             sys.api_version             sys.getdlopenflags(         sys.pydebug
sys.__getattribute__(       sys.argv                    sys.getfilesystemencoding(  sys.setcheckinterval(
sys.__hash__(               sys.builtin_module_names    sys.getprofile(             sys.setdlopenflags(
sys.__init__(               sys.byteorder               sys.getrecursionlimit(      sys.setprofile(
sys.__name__                sys.call_tracing(           sys.getrefcount(            sys.setrecursionlimit(
sys.__new__(                sys.callstats(              sys.getsizeof(              sys.settrace(
sys.__package__             sys.copyright               sys.gettrace(               sys.stderr
sys.__reduce__(             sys.displayhook(            sys.hexversion              sys.stdin
sys.__reduce_ex__(          sys.dont_write_bytecode     sys.long_info               sys.stdout
sys.__repr__(               sys.exc_clear(              sys.maxint                  sys.subversion
sys.__setattr__(            sys.exc_info(               sys.maxsize                 sys.version
sys.__sizeof__(             sys.exc_type                sys.maxunicode              sys.version_info
sys.__stderr__              sys.excepthook(             sys.meta_path               sys.warnoptions
sys.__stdin__               sys.exec_prefix             sys.modules                 
sys.__stdout__              sys.executable              sys.path                    
sys.__str__(                sys.exit(                   sys.path_hooks

 

python tab 自动补全

标签:

原文地址:http://www.cnblogs.com/lincj/p/5617969.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!