195

How can output to stdout be suppressed?

A semi-colon can be used to supress display of returned objects, for example

>>> 1+1
2

>>> 1+1;   # No output!

However, a function that prints to stdout is not affected by the semi-colon.

>>> print('Hello!')
Hello!

>>> MyFunction()
Calculating values...

How can the output from print / MyFunction be suppressed?

krassowski
  • 13,598
  • 4
  • 60
  • 92
Zero
  • 11,593
  • 9
  • 52
  • 70

4 Answers4

295

Add %%capture as the first line of the cell. eg

%%capture
print('Hello')
MyFunction()

This simply discards the output, but the %%capture magic can be used to save the output to a variable - consult the docs

Zero
  • 11,593
  • 9
  • 52
  • 70
  • 5
    Anyone know what the opposite of %%capture is? As in, how do you stop %%capture within a cell, or is it enabled until the end of the cell? – David Parks Sep 16 '16 at 17:18
  • 8
    Good thing to know is `%%capture` is only enabled until the end of the cell, and it must appear before any code in the cell. (So it appears there isn't a way to uncapture within a cell.) – Arel Oct 17 '16 at 19:32
  • ICYMI, @David Parks and @Arel's discussion is covered by @gwd2's answer on this page about using `with io.capture_output() as captured:` you can fine-tune within a cell to only capture what is run in that `with` context. – Wayne Feb 19 '20 at 17:23
  • Note this only works for Python kernels (e.g. not R). – Max Ghenis Feb 20 '20 at 05:47
  • 2
    It does not discard warnings, in my case I use tensorflow.keras and get their warnings. – till Kadabra Jul 26 '22 at 10:58
  • 1
    For your information, `%%capture cap` and `cap.show()` show the contents of the standard outputs. – dixhom Oct 09 '22 at 12:15
  • 1
    Uhh maybe this changed: UsageError: Line magic function `%%capture` not found. – sh37211 Oct 11 '22 at 21:10
140

Suppress output

Put a ; at the end of a line to suppress the printing of output [Reference].

A good practice is to always return values from functions rather than printing values inside a function. In that case, you have the control; if you want to print the returned value, you can; otherwise, it will not be printed just by adding a ; after the function call.

Farhad Maleki
  • 3,451
  • 1
  • 25
  • 20
  • 9
    This doesn't for a code inside a `for` loop. Any ideas? I only want to suppress output from particular lines of code in the cell, not all lines. Thanks – Confounded Nov 15 '19 at 11:15
  • 3
    this method doesn't work for `print()` at jupyter notebook. however, codes like `1+1;` works. – song.xiao Oct 03 '20 at 14:59
  • 3
    Also doesn't work for some 3rd party components; it seems to be supressing only native python stdout. – Kirk Broadhurst Oct 05 '20 at 00:16
  • 4
    did you not read the question, he explicitly said he wanted something else – Jules G.M. Jul 08 '21 at 23:18
  • 1
    Actually, jupyter will not print the value of the variable unless this is the last statement of the cell. If it is the last statement, the semicolon will suppress the output as you mentioned. – HAltos Nov 11 '21 at 01:07
  • Thank you so much! This helped prevent icecream's `ic` from printing twice. – Pyromonk Jun 07 '22 at 19:50
  • Can you please add an example? – Zioalex Jun 10 '22 at 11:46
  • this doesn't work when trying to run instabot :( Still get a bunch of garbage back cuz it's so broken. – Raksha Nov 30 '22 at 19:45
50

(credit: https://stackoverflow.com/a/23611571/389812)

You could use io.capture_output:

from IPython.utils import io

with io.capture_output() as captured:
    MyFunction()

to supress (e.g. capture) stdout and stderr for those lines within the with-statement.

gdw2
  • 7,558
  • 4
  • 46
  • 49
  • Thanks a lot, this was driving me crazy -- I have a startup function that I run every time I open a notebook that calls `%autosave`, and wanted to suppress its output. Thought it would be simple -- but weirdly, `contextlib.redirect_stdout` and `sys.stdout = open(os.devnull, 'w')` both fail (end up printing an extra blank line). This should be the accepted answer. – Luke Davis Nov 04 '18 at 18:20
  • 4
    This was my favourite answer, because one can suppress *some* of the output in a cell without suppressing all of it. – Wolpertinger Sep 10 '19 at 14:01
  • Exactly what I was looking for. – deltasata Jul 15 '21 at 07:06
  • thank you for saving a life – Matt Oct 12 '21 at 17:03
  • this doesn't work when trying to run instabot :( Still get a bunch of garbage back cuz it's so broken. – Raksha Nov 30 '22 at 19:44
  • Note that `capture_output(stdout=True, stderr=True)` lets you choose whether to capture `stdout`, `stderr` or both with the appropriate settings. – András Aszódi Aug 30 '23 at 12:24
-6

If anyone is interested in clearing all outputs:

  1. Go to Cell
  2. Go to All Output

Then choose whichever option you like.

Yuqin Peng
  • 59
  • 3