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.