如何将IPython控制台的输出保存到Spyder中的文件中? [英] How to save the output of an IPython console to a file in Spyder?

查看:561
本文介绍了如何将IPython控制台的输出保存到Spyder中的文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序中有一个错误:(

I have a bug in my program :(

问题在于:

  • 我的.py代码很长,并且要花一些时间才能运行
  • 我不知道错误在哪里
  • My .py code is long, and takes ages to run
  • I don't know where the bug is

好消息是,我的py文件中有很多print(),因此我有可能知道该漏洞的位置.

The good news is that I have a lot of print() in my py file, so I can potentially know where the bug lives.

坏消息是我的错误使计算机崩溃,因此我无法查看ipython控制台的输出并查看结果出了错.

The bad news is that my bug makes my computer crash, so there is no way for me to look at the output of the ipython console and see what went wrong.

在程序运行时如何将输出写入磁盘?这样重启后我仍然可以打开文件,以了解崩溃之前发生了什么事情?

How can I have the output be written to disk while the program runs? So that I can still open the file after reboot to understand what happened before the crash?

此问题不同于将stdout重定向到Python中的文件?,因为我需要

This question is different from Redirect stdout to a file in Python?, because I need

  • 连续写入文件
  • 在Spyder内部使用的东西

非常感谢!

推荐答案

一个非常有趣的问题!幸运的是,IPython为您提供了正确的魔术.它称为 %logstart (请点击以下链接)完整的文档).

Very interesting question! Fortunately IPython has the right magic for you. It's called %logstart (please follow the link for the full documentation).

要开始使用它并保存所有命令的输入和输出,只需在IPython控制台中键入

To start using it and save the input and output of all your commands, just type in an IPython console

In[1]: %logstart -o

,它将从那一刻起将您的会话记录到当前目录中的名为ipython_log.py的文件中.

and that will record your session from that moment on into a file called ipython_log.py placed in your current directory.

%logstart非常灵活,因此您可以选择要保存到的其他文件,以及保存会话的方式(纯Python或作为IPython命令).

%logstart is very flexible, so you can select a different file to save to, and also how you save your session (either pure Python or as IPython commands).

这篇关于如何将IPython控制台的输出保存到Spyder中的文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆