Python小技巧:Python3中利用tab键进行代码提示

前言
把这个文件导入到python自带的IDE下,再按TAB键就会有提示,需要readline库,在新的版本中,可能名字是gnureadline库,

需要安装 :

pip install gnureadline

pip install readline

也可以在pipy.python.org下载源码进行 /opt/python35/bin/python3 setup.py install 安装

但可能提示:依赖 ncurses ncurses-devel ,readline readline-devel

可以 yum install ncurses ncurses-devel readline readline-devel

cat tab.py 
#!/opt/python35/bin/python3
# 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

现在,只要导入import tab按tab就可以实现提示和补全了,

其实可以把该文件放到python程序的库路经。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章