Add auto-completion and command history to the python interactive command line

Source: Internet
Author: User

After the test, I started to study Python advanced programming.

Those who have used zsh will surely be impressed with its automatic complementing function. Similar functions can be achieved through simple custom Python interactive command lines. The specific operations are as follows:

    • Create the ". pythonstartup" file in the user directory and write the following content:
 #  Python Startup File  Import  Readline  Import  Rlcompleter  Import  Atexit  Import  OS  #  Tab completion Readline. parse_and_bind ( ' Tab: complete  '  )  #  History File Historyfile = OS. Path. Join (OS. Environ [ '  Home  ' ], '  . Pythonstartup  '  )  Try  : Readline. read_history_file (historyfile)  Except  : Pass  Atexit. Register (Readline. write_history_file, historyfile)  Del OS, historyfile, Readline, rlcompleter

 

    • Add environment variables, edit the. bashrc or. zshrc file (determined by your shell), and add the following content:
 
Export pythonstartup ="/Home/ma6174/. pythonstartup"

 

    • Open the terminal and go to the python interactive command line interface. Below is my running effect:

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.