Implementing Python in Ubuntu by tab completion

Source: Internet
Author: User

Just learn python, in fact, everything is very good to accept, because there is a C language Foundation, feel everything comes so natural, python extremely concise grammar, let me really love this language! I believe Python will be a great place to shine in my it career in the future!

OK, the crap is over, into the chase.

1. The problem arises: By default the TAB key of the Python interaction interface

Under Linux, or on the router, switch, press the TAB key to be very cool, what is not complete, tab out, helpless, in Linux installed in Python, the default is no tab function, that is, in the interactive interface of Python, tab is no way to complete, The Python interface simply treats it as a normal number of whitespace complements:

[Email protected]:~/seminar6/day1$ pythonpython 2.7.3 (default, 1, 05:14:39) [GCC 4.6.3] on Linux2type ' help ', '     Copyright "," credits "or" license "for more information.>>> import sys >>> sys. =====> Press the TAB key to see the Sys submodule, the result is to press a lot of space keys

Yes, it's disgusting! No Tab key, the baby is not happy!

2. Solution: Self-Import TAB key module--Create tab.py module file

The question is, what about the module without the TAB key? Baidu a bit! But at that time did find a lot, can not find a in my own experimental environment to use, always prompt various errors! Fortunately, finally let me find a can be used, the following directly gives the tab.py code:

#!/usr/bin/env python# python startup file import sysimport readlineimport rlcompleterimport atexitimport os# tab Completi On 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

Save the above code as a tab.py file and save it to one of the following specified paths:

>>> import sys>>> sys.path[', '/usr/lib/python2.7 ', '/usr/lib/python2.7/plat-linux2 ', '/usr/lib/ Python2.7/lib-tk ', '/usr/lib/python2.7/lib-old ', '/usr/lib/python2.7/lib-dynload ', '/usr/lib/python2.7/ Dist-packages ', '/usr/lib/python2.7/dist-packages/pil ', '/usr/lib/python2.7/dist-packages/gst-0.10 ', '/usr/lib/ python2.7/dist-packages/gtk-2.0 ', '/usr/lib/python2.7/dist-packages/ubuntu-sso-client ', '/usr/lib/python2.7/ Dist-packages/ubuntuone-client ', '/usr/lib/python2.7/dist-packages/ubuntuone-control-panel ', '/usr/lib/python2.7 /dist-packages/ubuntuone-couch ', '/usr/lib/python2.7/dist-packages/ubuntuone-installer ', '/usr/lib/python2.7/ Dist-packages/ubuntuone-storage-protocol ']

For the sake of easy, I put the tab.py file directly under the/usr/lib/python2.7 directory:

[Email protected]:~/seminar6/day1$ sudo mv Tab.py/usr/lib/python2.7/[email protected]:~/seminar6/day1$ ll/usr/lib/ python2.7/tab.py-rw-rw-r--1 xpleaf xpleaf 419 August 6 23:53/usr/lib/python2.7/tab.py

3. Scenario Test: After you import the tab module, you can use the TAB key to complete

You can use the TAB key Shuangshuang the following, but you should first import it in Python's interactive interface:

[email protected]:~/seminar6/day1$ pythonpython 2.7.3  (default, aug  1  2012, 05:14:39)  [GCC 4.6.3] on linux2Type  "Help",  "copyright",  " Credits " or " license " for more information.>>> import sys     ===> Import SYS module just for the next face tab completion test >>> import tab    === > This is the point >>> sys.          ===> Enter Sys. After pressing two times tab sys.__class__ (               sys.exit (sys.__delattr__ (             Sys.exitfunc (sys.__dict__                 sys.flagssys.__displayhook__ (        sys.float_infosys.__ Doc__                 sys.float_repr_stylesys.__excepthook__ (          sys.getcheckinterval (sys.__format__ (              sys.getdefaultencoding (sys.__getattribute__ (        sys.getdlopenflags (sys.__hash__ (                sys.getfilesystemencoding (sys.__init__ (                sys.getprofile (sys.__name__                 sys.getrecursionlimit (sys. __new__ (                 Sys.getrefcount (sys.__package__              Sys.getsizeof (sys.__reduce__ (&NBSp;            sys.gettrace (sys.__reduce_ex__ (           SYS.HEXVERSIONSYS.__REPR__ (                SYS.LONG_INFOSYS.__SETATTR__ (             sys.maxintsys.__sizeof__ (              sys.maxsizesys.__stderr__               sys.maxunicodesys.__stdin__                sys.meta_pathsys.__stdout__               SYS.MODULESSYS.__STR__ (                 sys.pathsys.__ subclasshook__ (    &nbsP;  sys.path_hookssys._clear_type_cache (      sys.path_importer_ Cachesys._current_frames (        sys.platformsys._getframe (               sys.prefixsys._mercurial               sys.ps1sys.api_version              sys.ps2sys.argv                      sys.py3kwarningsys.builtin_module_names    sys.pydebugsys.byteorder                sys.setcheckinterval (Sys.call_tracing (            sys.setdlopenflags (Sys.callstats (          &nBsp;    sys.setprofile (sys.copyright                sys.setrecursionlimit (Sys.displayhook (             sys.settrace (sys.dont_write_bytecode      Sys.stderrsys.exc_clear (               Sys.stdinsys.exc_info (                sys.stdoutsys.exc_type                 sys.subversionsys.excepthook (              sys.versionsys.exec_prefix              sys.version_infosys.executable               sys.warnoptions>>> sys. 

It feels great! Although every time I enter the interactive interface of Python to re-import the tab module, but there is always better than no! For beginners like me, have been very satisfied!

This article is from the "fragrant fluttering leaves" blog, please make sure to keep this source http://xpleaf.blog.51cto.com/9315560/1682449

Implementing Python in Ubuntu by tab completion

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.