温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

python交互模式下命令tab补全

发布时间:2020-07-12 15:45:29 来源:网络 阅读:1905 作者:牛奶i豆浆 栏目:系统运维

python默认就可以进行tab补全命令行,在交互模式下,只要自己写个小小的tab.py模块即可;实现代码如下;

#!/bin/env python 
# -*- coding: utf-8 -*-
# python startup file 
import sys
import readline
import rlcompleter
import atexit
import os
import platform
# tab completion 
readline.parse_and_bind('tab: complete')
## 此为增加历史命令记录到文件,在各自的家目录下,如果不需要记录日志可删除
if platform.system() == 'Windows':
    # history file ,os.environ获取用户的家目录,此为win10的,win7系统可能需要改下(自己看下os.environ的key)
    histfile = os.path.join(os.environ['USERPROFILE'], '.pythonhistory')
else:
    # history file ,os.environ获取用户的家目录
    histfile = os.path.join(os.environ['HOME'], '.pythonhistory')
## end for history###
try:
    readline.read_history_file(histfile)
except IOError:
    pass
atexit.register(readline.write_history_file, histfile)
del os, histfile, readline, rlcompleter

将以上代码复制出来保存到一个py文件中(自己定义名字,等下需要在交互下导入此模块),放入到你自己的py环境中搜索路径下即可

启动python交互

import xxx

然后你导入任意一个模块进行测试


如何你向在python启动的时候自动导入此模块定义下PYTHONSTARTUP环境变量将此模块加入到此环境变量中即可

    如果是windows系统的话,在自己的用户变量中定义(我的电脑==>属性==>高级==>环境变量==>用户变量)

   PYTHONSTARTUP 对应的值就是你刚才保存模块的路径即可

   如果你是linux的话,在自己的用户变量环境(/root/.bash_profile,或者全局变量中/etc/profile中加入export PYTHONSTARTUP=/xxx/xx/xx.py)中export模块的路径即可

   重载环境变量(重新登录下)即可测试


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI