Use python to customize interactive command lines

Source: Internet
Author: User

The Python interactive command line can be configured through the Startup File.

When Python is started, it looks for the environment variable PYTHONSTARTUP and executes the program code in the specified file in the variable. The specified file name and address can be arbitrary. When you press the Tab key, the content and command history are automatically supplemented. This effectively enhances the command line, and these tools are implemented based on the readline module (which requires the aid of the readline library ).

Here is an example of a simple STARTUP script file. It adds the key auto-completion content and history command functions to the python command line.

[python@python ~]$ cat .pythonstartupimport readlineimport rlcompleterimport atexitimport os#tab completionreadline.parse_and_bind('tab: complete')#history filehistfile = 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

Set Environment Variables

[python@python ~]$ cat .bash_profile|grep PYTHONexport PYTHONSTARTUP=/home/python/.pythonstartup

Verify the Tab key completion and view History commands.

[python@python ~]$ pythonPython 2.7.5 (default, Oct 6 2013, 10:45:13)[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2Type "help", "copyright", "credits" or "license" for more information.>>> import md5>>> md5.md5.__class__(     md5.__getattribute__( md5.__reduce__(    md5.__subclasshook__(md5.__delattr__(    md5.__hash__(     md5.__reduce_ex__(   md5.blocksizemd5.__dict__      md5.__init__(     md5.__repr__(     md5.digest_sizemd5.__doc__      md5.__name__      md5.__setattr__(    md5.md5(md5.__file__      md5.__new__(      md5.__sizeof__(    md5.new(md5.__format__(    md5.__package__    md5.__str__(      md5.warnings>>> import os>>> import md5

Note: If the following occurs during make:

Python build finished, but the necessary bits to build these modules were not found:_tkinter            gdbm      readline      sunaudiodev

If this is ignored, import readline reports an error. No module is specified!

The specified package is missing:

redhat:   readline-devel.xxx.rpm

Install and re-compile and execute, the problem can be solved.

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.