如何为Python终端提供持久性历史记录


Posted in Python onSeptember 03, 2019

问题

有没有办法告诉交互式Python shell在会话之间保留其执行命令的历史记录?

当会话正在运行时,在执行命令之后,我可以向上箭头并访问所述命令,我只是想知道是否有某种方法可以保存这些命令,直到下次我使用Python shell时。

这非常有用,因为我发现自己在会话中重用命令,这是我在上一个会话结束时使用的。

解决方案

当然你可以用一个小的启动脚本。来自python教程中的交互式输入编辑和历史替换:

# Add auto-completion and a stored history file of commands to your Python
# interactive interpreter. Requires Python 2.0+, readline. Autocomplete is
# bound to the Esc key by default (you can change it - see readline docs).
#
# Store the file in ~/.pystartup, and set an environment variable to point
# to it: "export PYTHONSTARTUP=~/.pystartup" in bash.

import atexit
import os
import readline
import rlcompleter

historyPath = os.path.expanduser("~/.pyhistory")

def save_history(historyPath=historyPath):
  import readline
  readline.write_history_file(historyPath)

if os.path.exists(historyPath):
  readline.read_history_file(historyPath)

atexit.register(save_history)
del os, atexit, readline, rlcompleter, save_history, historyPath

从Python 3.4开始,交互式解释器支持开箱即用的自动完成和历史记录:

现在,在支持的系统上的交互式解释器中默认启用Tab-completion readline。默认情况下也会启用历史记录,并将其写入(并从中读取)文件~/.python-history。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python常用正则表达式符号浅析
Aug 13 Python
使用Nginx+uWsgi实现Python的Django框架站点动静分离
Mar 21 Python
Python实现抓取HTML网页并以PDF文件形式保存的方法
May 08 Python
在Python中获取两数相除的商和余数方法
Nov 10 Python
opencv python统计及绘制直方图的方法
Jan 21 Python
python语言元素知识点详解
May 15 Python
Python 使用PyQt5 完成选择文件或目录的对话框方法
Jun 27 Python
flask框架配置mysql数据库操作详解
Nov 29 Python
Python ORM编程基础示例
Feb 02 Python
Pandas时间序列基础详解(转换,索引,切片)
Feb 26 Python
工程师必须了解的LRU缓存淘汰算法以及python实现过程
Oct 15 Python
Django+Django-Celery+Celery的整合实战
Jan 20 Python
Python openpyxl读取单元格字体颜色过程解析
Sep 03 #Python
python xlwt如何设置单元格的自定义背景颜色
Sep 03 #Python
python实现静态web服务器
Sep 03 #Python
Python的Lambda函数用法详解
Sep 03 #Python
Python获取一个用户名的组ID过程解析
Sep 03 #Python
python多线程实现TCP服务端
Sep 03 #Python
Python实现多线程/多进程的TCP服务器
Sep 03 #Python
You might like
php中print(),print_r(),echo()的区别详解
2014/12/01 PHP
PHP抓取及分析网页的方法详解
2016/04/26 PHP
PHP文件操作详解
2016/12/30 PHP
PHP基于递归算法解决兔子生兔子问题
2018/05/11 PHP
动态表格Table类的实现
2009/08/26 Javascript
js弹出框轻量级插件jquery.boxy使用介绍
2013/01/15 Javascript
javascript拖拽上传类库DropzoneJS使用方法
2013/12/05 Javascript
js星星评分效果
2014/07/24 Javascript
基于Jquery代码实现支持PC端手机端幻灯片代码
2015/11/17 Javascript
Bootstrap框架实现广告轮播效果
2016/11/28 Javascript
jQuery轻松实现无缝轮播效果
2017/03/22 jQuery
AngularJS 实现点击按钮获取验证码功能实例代码
2017/07/13 Javascript
详解vue.js下引入百度地图jsApi的两种方法
2018/07/27 Javascript
在Web关闭页面时发送Ajax请求的实现方法
2019/03/07 Javascript
小程序调用微信支付的方法
2019/09/26 Javascript
微信小程序静默登录的实现代码
2020/01/08 Javascript
vscode 使用Prettier插件格式化配置使用代码详解
2020/08/10 Javascript
实例讲解Python设计模式编程之工厂方法模式的使用
2016/03/02 Python
利用Python自动监控网站并发送邮件告警的方法
2016/08/24 Python
深入理解python对json的操作总结
2017/01/05 Python
遗传算法python版
2018/03/19 Python
python中计算一个列表中连续相同的元素个数方法
2018/06/29 Python
python用插值法绘制平滑曲线
2021/02/19 Python
python 实现批量替换文本中的某部分内容
2019/12/13 Python
Python Tkinter图形工具使用方法及实例解析
2020/06/15 Python
CSS3 clip-path 用法介绍详解
2018/03/01 HTML / CSS
美国男士西装打折店:Jos. A. Bank
2017/11/13 全球购物
法国在线宠物店:zooplus.fr
2018/02/23 全球购物
Hoka One One法国官网:美国专业跑鞋品牌
2018/12/29 全球购物
物流仓管员岗位职责
2013/12/04 职场文书
新学期校长寄语
2014/01/18 职场文书
井冈山红色之旅感想
2014/10/07 职场文书
毕业生见习报告总结
2014/11/08 职场文书
乡镇一岗双责责任书
2015/01/29 职场文书
个人落户申请书怎么写?
2019/06/28 职场文书
IIS服务器中设置HTTP重定向访问HTTPS
2022/04/29 Servers