美文网首页
python在交互式模式下tab键补全模块

python在交互式模式下tab键补全模块

作者: loveroot | 来源:发表于2016-12-30 09:56 被阅读140次
#!/usr/bin/python
# python tab feature,you must install readline package ,put this in your python dir and import tab to use it
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

保存为tab.py

====进入python,导入sys模块看看python的存放位置,然后将上面的文件放在python的目录下面,导入tab

>>> 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.py随意放在任意一个目录下面

[root@ca ~]# python

Python 2.6.6 (r266:84292, Nov 22 2013, 12:16:22)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import tab

>>> print

print print(

此时键入print按两下tab就会出来与print有关的命令。

相关文章

网友评论

      本文标题:python在交互式模式下tab键补全模块

      本文链接:https://www.haomeiwen.com/subject/frcmvttx.html