红联Linux门户
Linux帮助

Python中的tab补全

发布时间:2015-05-23 09:49:23来源:linux网站作者:olinux

首先需要知道Python的读取路径

>>> 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/lib/python2.6/site-packages']


可以看到python可以去这些地方读取,我们把脚本放在/usr/lib64/python2.6 即可

[root@python python2.6]# cat startup.py
#!/usr/bin/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


这样做一个问题就是,你每次启动python后,都需要手动导入一下,比如

>>> import startup


可以在系统环境变量中,加入读取路径,这样就免去了每次导入的麻烦

[root@python python2.6]# cat ~/.bashrc

export PYTHONSTARTUP=/usr/lib64/python2.6/startup.py

即可。


无需操作系统直接运行Python代码:http://www.linuxdiyf.com/linux/12104.html

Ubuntu 14.04下OpenCV 3.0+Python 2.7安装测试:http://www.linuxdiyf.com/linux/11903.html

教你用Python创建瀑布图:http://www.linuxdiyf.com/linux/11549.html

开启Vim的Python支持:http://www.linuxdiyf.com/linux/11381.html

如何在Vim中使用tab进行Python代码补全:http://www.linuxdiyf.com/linux/10130.html