51

I get some useless warnings in python 3 jupyter notebook. I want to turn off these warnings in a particular cell only, so not in the rest of the ipynb-file. Does someone know how to do this?

Marnix
  • 719
  • 1
  • 6
  • 14
  • You can temporarily turn off all Python warnings with the [catch_warnings()](https://docs.python.org/3/library/warnings.html#warnings.catch_warnings) context manager. – Thomas K Oct 18 '16 at 12:20
  • 1
    Does this answer work for you? Using this line `warnings.filterwarnings(action='once')` from this answer: https://stackoverflow.com/a/9031848/4538920 – Ahmed Shendy Aug 24 '19 at 10:52
  • "*I want to turn off these warnings in a particular cell only*": Unless you have a single instruction in the cell, you may want to disable warnings for *some* instructions, not for the *cell*. – mins Apr 13 '21 at 10:35

6 Answers6

35

Write %%capture as the first line of the cell to catch cell output. You can use the options --no-stderr, --no-stdout, --no-display, and --output to control which cell outputs will be caught. See more details here.

Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
Joshua Dempster
  • 556
  • 4
  • 9
11

Solutions:

Usually there is no need to extend the effect to the whole cell, as this may hide some other useful message, so use a context manager to ignore the warnings:

with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    # Your problematic instruction(s) here

If there are good reasons to protect the whole cell then simply prevent the stderr stream to be displayed in the cell output area by inserting the capture "magic":

%%capture --no-stderr

at the top of the cell (rather than indenting all lines).


Explanations:

The most logical way is to insert the code triggering a warning within a context manager introduced by keyword with. It switches off and restore the warning system prior and after the problematic code

Python provides such context manager as warnings.catch_warnings:

A context manager that copies and, upon exit, restores the warnings filter and the showwarning() function. If the record argument is False (the default) the context manager returns None on entry. If record is True, a list is returned that is progressively populated with objects as seen by a custom showwarning() function (which also suppresses output to sys.stdout).

You also need to register a filter to react to each warning captured by the context manager, else the default filter remains active.

Python provides the warnings.simplefilter which:

Insert a simple entry into the list of warnings filter specifications

It registers some action (ignore for no action, always to print the warning message, etc, see full description).

Example:

import warnings, math
import numpy as np

# We want to compute the logs of this sequence.
# But log isn't defined for values <= 0
s = [-1, 5, 2, 0]

with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    r = np.log(s)

print(r)
pixelistik
  • 7,541
  • 3
  • 32
  • 42
mins
  • 6,478
  • 12
  • 56
  • 75
  • Note that with `%%capture`, the param `--no-stderr` means to **not prevent** stderr from output (rather, everything else). – pixelistik Jan 16 '23 at 20:47
10

Normally I want to keep stdout open for printing so I find using catch_warnings like this better:

import warnings

def action_with_warnings():
    warnings.warn("should not appear")

with warnings.catch_warnings(record=True):
    action_with_warnings()

It has the disadvantage of storing the warnings in memory, but it is normally not a significant overhead and worth the simplicity. Even within a single cell I find having fine grained control means warnings I do care about are not accidentlly missed.

typingduck
  • 825
  • 10
  • 15
2

I tried many different commands, but this one works for me

import warnings

warnings.filterwarnings('ignore')
n4321d
  • 1,059
  • 2
  • 12
  • 31
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 30 '22 at 15:52
1

For me these two lines do the job:

import warnings
warnings.filterwarnings('ignore')
happyhuman
  • 1,541
  • 1
  • 16
  • 30
1

I added %%capture --no-stdout as the first line of the cell. That worked for me. Strangely, %%capture --no-stderr showed only the warnings, but none of the desired output.

irene
  • 2,085
  • 1
  • 22
  • 36