Command tab completion in Python interactive mode

Source: Internet
Author: User

Python By default can be tab completion command line, in interactive mode, as long as you write a small tab.py module, the implementation code is as follows;

#!/bin/env python # -*- coding: utf-8 -*-# python startup file  import sysimport readlineimport rlcompleterimport atexitimport osimport  Platform# tab completion readline.parse_and_bind (' Tab: complete ') ##  this for adding historical commands to the file, In the respective home directory, if you do not need to log the logs can be deleted If platform.system ()  ==  ' Windows ':     # history  file ,os.environ get the user's home directory, this is WIN10, Win7 system may need to change (see Os.environ key yourself)     histfile  = os.path.join (os.environ[' userprofile '],  '. Pythonhistory ') else:    #  history file ,os.environ gets the user's home directory     histfile = os.path.join ( os.environ[' HOME '],  '. Pythonhistory ') ## end for history## #try:     Readline.read_history_file (histfile) Except ioerror:    passatexit.register ( Readline.write_history_file, histfile) dEl os, histfile, readline, rlcompleter 

Copy the above code to save in a py file (your own name, and so on you need to import this module interactively), into your own PY environment in the search path can be

Start Python interaction

Import xxx

Then you import any one of the modules to test


How do you automatically import this module in Python when you start the PYTHONSTARTUP environment variable to add this module to this environment variable

If it is a Windows system, define it in your own user variable(我的电脑==>属性==>高级==>环境变量==>用户变量)

   PYTHONSTARTUPThe corresponding value is the path of the module you just saved.

If you are Linux, the path to the export module in your own user-variable environment ( /root/.bash_profile or global variable/etc/export export PYTHONSTARTUP=/xxx/xx/xx.py )

Overload environment variables (re-login) to test


Command tab completion in Python interactive mode

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.