請(qǐng)參考個(gè)人博客
python配置tab自動(dòng)不全
說(shuō)明
有時(shí)候Centos系統(tǒng)默認(rèn)安裝的python進(jìn)入交互模式下不能使用tab快捷鍵功能,這個(gè)時(shí)候需要自己進(jìn)行相關(guān)配置
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
配置
這里以python.27為例,把
- 上面的tab.py 放到 /usr/lib/python2.7/site-packages/ 下面,需要先導(dǎo)入sys,再倒入tab模塊
- 可以把tab.py的內(nèi)容放到
/root/.pythontab
文件中,然后在/root/.bash_profile
中添加export PYTHONSTARTUP=~/.pythontab
, 這種方式不需要再次導(dǎo)入 tab 模塊
測(cè)試
root@pts/2 $ python
Python 2.7.5 (default, Sep 15 2016, 22:37:39)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> import tab
>>> sys.
sys.__class__( sys.__stdout__ sys.executable sys.path
sys.__delattr__( sys.__str__( sys.exit( sys.path_hooks
sys.__dict__ sys.__subclasshook__( sys.exitfunc( sys.path_importer_cache
附加
PYTHONSTARTUP 官網(wǎng)解釋:
If this is the name of a readable file, the Python commands in that file are executed before the first prompt is displayed in interactive mode. The file is executed in the same namespace where interactive commands are executed so that objects defined or imported in it can be used without qualification in the interactive session. You can also change the prompts sys.ps1 and sys.ps2 in this file.
簡(jiǎn)單來(lái)說(shuō)就是這個(gè)文件會(huì)在第一次進(jìn)入交互模式的時(shí)候會(huì)被執(zhí)行,所以把tab.py 加入到這個(gè)變量設(shè)定的文件中就可以達(dá)到自動(dòng)導(dǎo)入tab.py,實(shí)現(xiàn)自動(dòng)補(bǔ)全的功能