5

I am running a Python script in Jupyter Notebook and a library I am using (PyNN), produces a lot of stderr output that slows down the code and fills up the memory. I have tried using %%capture at the beginning of the cell but nothing has changed, and the output remains the same.

I am posting a snap of the output.enter image description here

Any tips are appreciated. Thanks

costisst
  • 381
  • 2
  • 6
  • 22

2 Answers2

2

If the issue is with printing, you can redirect the stream with contextlib:

import contextlib
import os

devnull = open(os.devnull, 'w')
contextlib.redirect_stderr(devnull)
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • I am having for than 1 scripts in my code, which implement different classes. I tried this code in the main script, which is the only one using the PyNN library, but it didn't work. I also tried adding it in every script and it didn't work either – costisst Aug 23 '20 at 13:33
  • Bonus tip: If you want to temporarily redirect the output (e.g. for just one fucntion call) you can use it as a context manager: `with redirect_stdout(open(os.devnull,'w')):`. Works for stderr and stdout. – mad Dec 22 '21 at 13:45
  • Does that actually close the devnull handle @mad – Maximilian Burszley Dec 29 '21 at 23:29
  • You may need multiple handlers as such: `with open(os.devnull, 'w') as h, redirect_stdout(h):` to properly close the contexts. – Maximilian Burszley Dec 29 '21 at 23:29
  • @Maximilian-Burszley: Good observation. I'm not sure if this is the right way to check whether the handler is closed, but I used `psutil.Process().open_files()` and saw nothing. I also tried it with an actual file inside `open()` instead of `os.devnull`, same result. So it appears to me that the file handler is indeed properly closed with the nested `open()`. – mad Jan 10 '22 at 11:38
  • This is not working for me: still tons of stderr spewage – WestCoastProjects Aug 26 '22 at 03:03
0

I use css to workaround:

%%html
<style>
    div.output_stderr {
    display: none;
    }
</style>