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