Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write print statements including errors/warnings to file in Spyder [duplicate]

Tags:

I have a big python script where multiple print statements and warnings (e.g. deprecation warnings) are being printed to the IPython console. For debugging purposes I want to write all print statements and warnings/error to an output file. Is there a way to do this in Spyder?

I have tried in the console (but it won't create an output file in any of my directories):

runfile('pyfile.py',wdir='wdir') > output.txt

I know how to do this in the prompt:

python "directory\pyfile.py"> output.txt 2>&1

But being able to do this in Spyder would be way more convenient. I have conducted an extensive search here on Stack Overflow but none of the answers to previous questions gave me the desired result.

like image 431
Bollehenk Avatar asked Sep 18 '18 12:09

Bollehenk


1 Answers

(Spyder maintainer here) You need to be aware that Spyder uses IPython consoles to run your code, so it's always better to search for what things you can do in IPython instead of in Spyder.

In this case, searching for ipython save stdout gave me the right answer in the first result, and that's an StackOverflow one.

In essence, you need to use the %%capture magic or a context manager (as the one mentioned in the answer above) and put runfile inside one of them to capture all stdout output in your program.

like image 112
Carlos Cordoba Avatar answered Oct 11 '22 18:10

Carlos Cordoba