新手学习python,经常要使用python命令行查找一些不熟悉的使用方法等等,但是python命令行下没有自带tab补全的功能,着实让我这新手菜了....不过这好在是个互联网的时代,没有多大的事,互联一下,特此记录。
vi tab.py#!/usr/bin/env python # python startup file import sysimport readlineimport rlcompleterimport atexitimport 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: passatexit.register(readline.write_history_file, histfile)del os, histfile, readline, rlcompleter
代码有了,我们还需要将脚本放到python指定的目录下,可以使用sys.path来查看一下
>>> import sys>>> sys.path['', '/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', '/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/ubuntu-sso-client']
一般我们会将这一类代码放在/usr/local/lib/python2.7/dist-packages目录下
mv tab.py /usr/local/lib/python2.7/dist-packages
ok,下面我们可以直接导入tab模块,马上体验一下tab补全吧.....
>>> import tab>>> sys.sys.__class__( sys.__subclasshook__( sys.exitfunc( sys.path_importer_cachesys.__delattr__( sys._clear_type_cache( sys.flags sys.platformsys.__dict__ sys._current_frames( sys.float_info sys.prefixsys.__displayhook__( sys._getframe( sys.float_repr_style sys.ps1sys.__doc__ sys._mercurial sys.getcheckinterval( sys.ps2sys.__excepthook__( sys._multiarch sys.getdefaultencoding( sys.py3kwarningsys.__format__( sys.api_version sys.getdlopenflags( sys.pydebugsys.__getattribute__( sys.argv sys.getfilesystemencoding( sys.setcheckinterval(sys.__hash__( sys.builtin_module_names sys.getprofile( sys.setdlopenflags(sys.__init__( sys.byteorder sys.getrecursionlimit( sys.setprofile(sys.__name__ sys.call_tracing( sys.getrefcount( sys.setrecursionlimit(sys.__new__( sys.callstats( sys.getsizeof( sys.settrace(sys.__package__ sys.copyright sys.gettrace( sys.stderrsys.__reduce__( sys.displayhook( sys.hexversion sys.stdinsys.__reduce_ex__( sys.dont_write_bytecode sys.long_info sys.stdoutsys.__repr__( sys.exc_clear( sys.maxint sys.subversionsys.__setattr__( sys.exc_info( sys.maxsize sys.versionsys.__sizeof__( sys.exc_type sys.maxunicode sys.version_infosys.__stderr__ sys.excepthook( sys.meta_path sys.warnoptionssys.__stdin__ sys.exec_prefix sys.modules sys.__stdout__ sys.executable sys.path sys.__str__( sys.exit( sys.path_hooks
所有的功能都能很直观的看到,如果想了解具体用法,可以使用help命令来自己查看帮助信息。