I have a bug in my program :(
The problem is that:
.py
code is long, and takes ages to runThe 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
Many thanks!
First, use print(x), or plt. show() if using matplotlib. pyplot. Then, if you find that spyder python does not give any output at (F5), change Preferences (spanner icon), select menu 'Run' and change to a different Console option, e.g. 'Execute in a dedicated console', then Run (F5).
Connecting to a console. Spyder can launch new IPython instances itself, through “Open an IPython console” under the Consoles menu, the IPython Console pane menu or its context menu ( Ctrl - T by default), to take advantage of the full suite of Spyder's features.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With