新手必学的使用命令行实现tab键自动补全功能[python头条资讯]
具体实现步骤如下
1.编写具体的实现tab补全功能的脚本,名为tab.py
#!/usr/bin/python# python tab complete
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的模块路径
>>> import sys>>> sys.path
['', '/usr/lib64/python27.zip', '/usr/lib64/python2.7', '/usr/lib64/python2.7/plat-linux2',
'/usr/lib64/python2.7/lib-tk', '/usr/lib64/python2.7/lib-old', '/usr/lib64/python2.7/lib-dynload',
'/usr/lib64/python2.7/site-packages', '/usr/lib/python2.7/site-packages']
>>>
python的模块放在了/usr/lib64/python27下面,将脚本拷贝到该目录下,在使用时导入即可。
3.导入tab
>>> import tab
>>> import os
>>> os.
Display all 249 possibilities? (y or n)
4.但python读取模块的路径顺序优先是从当前目录开始,所以若是当前目录也存在tab.py,但内容不同的python脚本,则可能会报错,所以在环境变量中也指定tab.py脚本
#for pythonexport PYTHONSTARTUP=/usr/lib64/python2.7/tab.py
以上是 新手必学的使用命令行实现tab键自动补全功能[python头条资讯] 的全部内容, 来源链接: utcz.com/z/527350.html