Python implements custom interactive command-line methods

Source: Internet
Author: User
The interactive command line for Python can be configured through a startup file.

When Python starts, it looks for the environment variable Pythonstartup and executes the program code in the file specified in the variable. The specified file name and address can be arbitrary. Pressing the TAB key automatically complements the content and command history. This is an effective enhancement to the command line, which is implemented based on the ReadLine module (which requires a ReadLine library-assisted implementation).

Here's a simple example of a startup script file that adds a button auto-complete content and history command functionality 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

Setting environment variables

[Python@python ~]$ Cat. Bash_profile|grep Pythonexport Pythonstartup=/home/python/.pythonstartup

Verify tab completion and historical command viewing.

[Python@python ~]$ Pythonpython 2.7.5 (default, Oct 6, 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__ ( c0/>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 make is present:

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

If this is ignored, import ReadLine will make an error. Indicates no module specified!

Here is the missing specified package:

Redhat:   readline-devel.xxx.rpm

The problem can be resolved by recompiling the installation.

  • 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.