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?
-
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
-
1Does 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
3 Answers
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.
- 43,885
- 25
- 111
- 113
- 500
- 4
- 9
-
15
-
1FWIW to answer the question directly, adding `%%capture output` at the top of the cell in Jupyter notebook seemed to work for me. – Vincent Jun 09 '20 at 02:55
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.
- 769
- 8
- 14
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 therecordargument isFalse(the default) the context manager returnsNoneon entry. IfrecordisTrue, a list is returned that is progressively populated with objects as seen by a customshowwarning()function (which also suppresses output tosys.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)
- 5,216
- 10
- 48
- 66