Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Command tab completion in python interactive mode

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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

#!/ bin/env python # -*- coding: utf-8-*-# python startup file import sysimport readlineimport rlcompleteimport atexitiimport osimport platform# tab completion readline.parse_and_bind ('tab: complete')##This is to add historical commands to the file, in the respective home directory, if you do not need to log can delete if platform.system() == 'Windows': # history file ,os.environ Get user's home directory, this is win10, win7 system may need to change (see os.environ key for yourself) histfile = os.path.join(os.environ['USERPROFILE'], '.pythonhistory')else: # history file ,os.environ Get 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 and save it to a py file (define the name yourself, and later you need to import this module under interaction), and put it into your py environment under the search path.

Start Python interaction

import xxx

And then you import any module and you test it.

How do you automatically import this module into Python when it starts? Under the PYTHONSTARTUP environment variable, add this module to this environment variable

If it is windows system, define it in your own user variables (my computer ==> properties ==> advanced ==> environment variables ==> user variables)

PYTHONSTARTUP corresponds to the path of the module you just saved

If you are linux, add export PYTHONSTARTUP=/xxx/xx/xx.py to your own user variable environment (/root/.bash_profile, or/etc/profile in the global variable)

Overload environment variables (under login again) can be tested

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report