python交互式命令_python实现定制交互式命令行的方法

本文介绍了如何通过设置PYTHONSTARTUP环境变量来增强Python的交互式命令行体验,包括启用Tab自动补全和历史命令功能。通过导入readline和rlcompleter模块,并配置启动脚本,可以实现这些功能。同时,文章提醒用户注意可能缺少的readline库,需要安装相应的开发包以避免导入错误。
摘要由CSDN通过智能技术生成

Python的互动式cmd可根据起动文档来配备。

当Python启动,会搜索系统变量PYTHONSTARTUP,而且实行该自变量中所特定文档里的编程代码。该特定文件目录及其详细地址能够是随便的。按Tab键的时候会全自动补齐內容和指令历史时间。这对cmd的合理提高,而这种专用工具则是根据readline控制模块完成的(这必须readline程序库輔助完成)。

这里为大伙儿举一个简易的起动脚本文件事例,它为pythoncmd加上了功能键全自动补齐內容和历史时间指令作用。[python@python ~]$ cat .pythonstartupimport readlineimport rlcompleterimport atexitimport os#tab completionreadline.parse_and_bind('tab: complete')#history filehistfile = os.path.join(os.environ['HOME'], '.pythonhistory')try: readline.read_history_file(histfile)except IOError: passatexit.register(readline.write_history_file,histfile)del os,histfile,readline,rlcompleter

设定系统变量[python@python ~]$ cat .bash_profile|grep PYTHONexport PYTHONSTARTUP=/home/python/.pythonstartup

认证Tab键补齐和历史时间指令查询。[python@python ~]$ pythonPython 2.7.5 (default, Oct 6 2013, 10:45:13)[GCC 4.1.2 20080704 (Red Hat 4.1.2-44)] on linux2Type "help", "copyright", "credits" or "license" for more information. import md5 md5.md5.__class__(md5.__getattribute__( md5.__reduce__( md5.__subclasshook__(md5.__delattr__( md5.__hash__(md5.__reduce_ex__( md5.blocksizemd5.__dict__md5.__init__(md5.__repr__(md5.digest_sizemd5.__doc__md5.__name__md5.__setattr__( md5.md5(md5.__file__md5.__new__(md5.__sizeof__( md5.new(md5.__format__( md5.__package__ md5.__str__(md5.warnings import os import md5

留意:假如在make的情况下出現:Python build finished, but the necessary bits to build these modules were not found:_tkintergdbmreadlinesunaudiodev

假如对于此事忽视了得话,import readline会出错。表明沒有特定控制模块!

这儿是缺乏特定包:redhat: readline-devel.xxx.rpm

安裝上再次编译程序实行,难题就可以获得处理。文章内容来源于:www.seo-7.com

(责任编辑:网络)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值