学习python时经常需要在Mac终端下实时运行python,习惯了PyCharm的自动补全,在终端上写python一个个字母敲很是折磨,如是找到了一个自动补全的方法。
在site-packages中添加一个tab模块,每次引入即可。
找到你的site-packages位置
>>> import sys, pprint
>>> pprint.pprint(sys.path)
['',
'/Library/Python/2.7/site-packages/Django-1.10.6-py2.7.egg',
这里'/Library/Python/2.7/site-packages'就是模块包位置
创建tab.py模块
touch /Library/Python/2.7/site-packages/tab.py
vi /Library/Python/2.7/site-packages/tab.py
写入tab.py代码
#!/usr/bin/python
import readline,rlcompleter
class TabCompleter(rlcompleter.Completer):
"""Completer that supports indenting"""
def complete(self, text, state):
if not text:
return (' ', None)[state]
else:
return rlcompleter.Completer.complete(self, text, state)
readline.set_completer(TabCompleter().complete)
if 'libedit' in readline._doc_:
readline.parse_and_bind("bind -e")
readline.parse_and_bind("bind '\t' rl_complete")
else:
readline.parse_and_bind("tab: complete")
import os
histfile = os.path.join(os.environ["HOME"], ".pyhist")
try:
readline.read_history_file(histfile)
except IOError:
pass
import atexit
atexit.register(readline.write_history_file, histfile)
del histfile
终端效果显示
>>> import tab, copy
>>> copy. ###按下Tab两次之后显示如下
604E23C0-C768-4FB7-BF99-7A62B62B7538.png