python tab complete

一、查询python安装路径,一般默认是/usr/bin/

moonx@moonx:/usr/download/test/mypython$ python

Python 2.7.12 (default, Oct 8 2019, 14:14:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/opt/stack/keystone', '/opt/stack/glance', '/opt/stack/neutron', '/opt/stack/nova', '/opt/stack/horizon', '/opt/ros/kinetic/lib/python2.7/dist-packages', '/usr/lib/python2.7', '/usr/lib/python2.7/plat-x86_64-linux-gnu', '/usr/lib/python2.7/lib-tk', '/usr/lib/python2.7/lib-old', '/usr/lib/python2.7/lib-dynload', '/home/moonx/.local/lib/python2.7/site-packages', '/usr/local/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages', '/usr/lib/python2.7/dist-packages/PILcompat', '/usr/lib/python2.7/dist-packages/gtk-2.0', '/usr/lib/python2.7/dist-packages/wx-3.0-gtk2']
>>>

二、切换到python的安装目录下,编辑tab键补全模块

  moonx@moonx:/usr/download/test/mypython$ cd /usr/local/lib/python2.7/
  moonx@moonx:/usr/local/lib/python2.7$ pwd
  /usr/local/lib/python2.7
  moonx@moonx:/usr/local/lib/python2.7$ vi 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

三、修改~/.bashrc

moonx@moonx:/usr/local/lib/python2.7$ echo "export PYTHONSTARTUP=/usr/local/lib/python2.7/tab.py" >> ~/.bashrc
moonx@moonx:/usr/local/lib/python2.7$ source ~/.bashrc

四、进入python测试,可正常使用tab补全功能了

moonx@moonx:/usr/download/test/mypython$ python
Python 2.7.12 (default, Oct 8 2019, 14:14:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import simple_package
>>> simple_package

原文地址:https://www.cnblogs.com/cjyp/p/11764075.html