0

I am trying to suppress the print statements in a function imported from a library. I use Python 3.6 in Jupyter Notebook.

I used:

sys.stdout = open(os.devnull, "w")
x = myfunction()
sys.stdout = sys.__stdout__

Unfortunately this succeeds in suppressing the print statements, but not in reinstating them afterwards. So I cannot print anything after this unless I restart the kernel.

How could I fix this?

Alexis Eggermont
  • 7,665
  • 24
  • 60
  • 93
  • I tried this and the print works after third line (sys.stdout = sys.__stdout__) . – vishal May 07 '18 at 11:12
  • @serbia99 strange... are you also on jupyter notebook? – Alexis Eggermont May 07 '18 at 11:43
  • 1
    @AlexisEggermont jupyter notebook uses IPython under the hood, which overwrites `sys.stdout`. You will need to keep a copy of the old stdout and restore using that rather than using `sys.__stdout__`. – Dunes May 07 '18 at 12:01

0 Answers0