How to save the output of an IPython console to a file in Spyder?

风流意气都作罢 提交于 2019-11-29 14:52:01

问题


I have a bug in my program :(

The problem is that:

  • My .py code is long, and takes ages to run
  • I don't know where the bug is

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

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?

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

  • continuous writing to file
  • something to use from within Spyder

Many thanks!


回答1:


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

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

In[1]: %logstart -o

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

%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).



来源:https://stackoverflow.com/questions/40754285/how-to-save-the-output-of-an-ipython-console-to-a-file-in-spyder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!