Add auto-completion and history functions to PythonIDLE

Source: Internet
Author: User
This article mainly introduces how to add auto-completion and history functions to PythonIDLE. For more information, see Python, I prefer to enter several lines of code in IDLE (interactive prompt mode. However, it is not convenient to edit the code in this mode. The most important thing is that you cannot use the Tab to automatically complete the code and remember the last input command (no way, who made us get used to it in Shell ).
At this time, we can directly use Python to start the script to solve this problem.

The procedure for starting the script is very simple. the code is provided here:

Import readlineimport rlcompleterimport atexitimport OS # tab autocompletereadline. parse_and_bind ('Tab: complete') # history filehistfile = OS. path. join (OS. environ ['home'], '. pythonhistory ') try: readline. read_history_file (histfile) handle T IOError: passatexit. register (readline. write_history_file, histfile) del OS, histfile, readline, rlcompleter

After that, save it as. pythonstartup, store it in your own directory (for example,/home/yurii), and point the PYTHONSTARTUP variable to the address you just put. The most convenient way is to add such a line in bashrc:

The code is as follows:


Export PYTHONSTARTUP =/home/yurii/. pythonstartup

In this way, it not only adds the tab auto-completion function, but also enables you to go to the last input command through the up and down keys when you restart IDLE, which is very convenient.

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.