請參考個人博客
python配置tab自動不全
說明
有時候Centos系統(tǒng)默認(rèn)安裝的python進(jìn)入交互模式下不能使用tab快捷鍵功能壳炎,這個時候需要自己進(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 模塊
測試
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.
簡單來說就是這個文件會在第一次進(jìn)入交互模式的時候會被執(zhí)行毡咏,所以把tab.py 加入到這個變量設(shè)定的文件中就可以達(dá)到自動導(dǎo)入tab.py陋率,實(shí)現(xiàn)自動補(bǔ)全的功能