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
6 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.

- 56,955
- 33
- 144
- 158

- 556
- 4
- 9
-
22
-
4FWIW 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
-
-
-
-
@glmorous that's probably why the commands don't work. I don't think all standard Jupyter magic commands work in vscode. – Trenton McKinney Mar 15 '23 at 22:27
-
1@TrentonMcKinney, oh, I didn't know that. Since some do work, I guess I thought that they all work. Thanks. – glmorous Mar 16 '23 at 23:10
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 therecord
argument isFalse
(the default) the context manager returnsNone
on entry. Ifrecord
isTrue
, 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)

- 7,541
- 3
- 32
- 42

- 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
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.

- 825
- 10
- 15
I tried many different commands, but this one works for me
import warnings
warnings.filterwarnings('ignore')

- 1,059
- 2
- 12
- 31

- 71
- 5
-
1Your 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
For me these two lines do the job:
import warnings
warnings.filterwarnings('ignore')

- 1,541
- 1
- 16
- 30
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.

- 2,085
- 1
- 22
- 36