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

查看:25
本文介绍了如何将 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?

这个问题不同于 将标准输出重定向到 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天全站免登陆