14

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!

Community
  • 1
  • 1
ℕʘʘḆḽḘ
  • 18,566
  • 34
  • 128
  • 235
  • 1
    Write to a file instead of using print statements? Use the Logger [module](https://docs.python.org/2/library/logging.html)? Use the debugger instead of print statements? Oh, actually just remembered another [post](http://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python) – Albert Rothman Nov 23 '16 at 01:17
  • Thanks @AlbertRothman but I have no idea how to do that. Can you post that as an answer and explain what to do please? – ℕʘʘḆḽḘ Nov 23 '16 at 01:19
  • 1
    Yeah, your question is pretty much a duplicate of the post I linked above. Look there. – Albert Rothman Nov 23 '16 at 01:21
  • thanks but that does not work. I want to see at the errors thrown by `pandas` and other. and any other output in the console. my `print` statements allow me to understand were I am in the code, but I need the full text in the console – ℕʘʘḆḽḘ Nov 23 '16 at 01:24
  • 1
    then also redirect the std err to file using the same method as redirecting the std out – Albert Rothman Nov 23 '16 at 01:25
  • please just post the code ! :D – ℕʘʘḆḽḘ Nov 23 '16 at 01:28

1 Answers1

22

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

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
  • 1
    Thanks! IPython has a lot of little treasures like this one :-) – Carlos Cordoba Nov 23 '16 at 16:46
  • 1
    @carloscoroba on a side note, any new version of anaconda coming soon? thanks again for your great work – ℕʘʘḆḽḘ Nov 23 '16 at 16:47
  • 2
    Anaconda 4.3 will come out the last week of January or first of February 2017, and it'll include Spyder 3.1. – Carlos Cordoba Nov 23 '16 at 17:04
  • 10
    @CarlosCordoba, it seems `%logstart -o` only logs out put if there is `Out[n]`, but if there is a `stdout` it doesn't capture that. Is there a way to capture the `stdout` in `ipython`, especially if there is no `Out[n]`? – alpha_989 Jun 17 '18 at 02:26