534

I need to produce a screencast of an IPython session, and to avoid confusing viewers, I want to disable all warnings emitted by warnings.warn calls from different packages. Is there a way to configure the ipythonrc file to automatically disable all such warnings?

0

7 Answers 7

1268

Place:

import warnings warnings.filterwarnings('ignore') 

inside ~/.ipython/profile_default/startup/disable-warnings.py.

Quite often it is useful to see a warning once. This can be set by:

warnings.filterwarnings(action='once') 
Sign up to request clarification or add additional context in comments.

8 Comments

Also works for IPython notebook warnings. Nice fix :)
@FrozenFlame, you change it back to 'default' See: docs.python.org/2/library/warnings.html
You can also execute this in the a notebook, in order to suppress only (certain) warnings in one specific notebook
Something odd happens to me, I work on Kaggle notebook, and even if I set warnings.filterwarnings('ignore') at the beginning of my script, I get warnings anyway. Should it be related to the fact that I use TPU accelerator, does TPU have a particular behaviour in this case ... I wonder. I do not understand.
Just make sure to invoke at the beginning of the code!
|
65

I hide the warnings in the pink boxes by running the following code in a cell:

from IPython.display import HTML HTML('''<script> code_show_err=false; function code_toggle_err() { if (code_show_err){ $('div.output_stderr').hide(); } else { $('div.output_stderr').show(); } code_show_err = !code_show_err } $( document ).ready(code_toggle_err); </script> To toggle on/off output_stderr, click <a href="javascript:code_toggle_err()">here</a>.''') 

Comments

15

The accepted answer does not work in Jupyter (at least when using some libraries).

The JavaScript solutions here only hide warnings that are already showing but not warnings that would be shown in the future.

To hide/unhide warnings in Jupyter and JupyterLab I wrote the following script that essentially toggles CSS to hide/unhide warnings.

%%javascript (function(on) { const e = $("<a>Setup failed</a>"); const ns = "js_jupyter_suppress_warnings"; var cssrules = $("#" + ns); if(!cssrules.length) cssrules = $("<style id='" + ns + "' type='text/css'>div.output_stderr { } </style>").appendTo("head"); e.click(function() { var s = 'Showing'; cssrules.empty() if(on) { s = 'Hiding'; cssrules.append("div.output_stderr, div[data-mime-type*='.stderr'] { display:none; }"); } e.text(s + ' warnings (click to toggle)'); on = !on; }).click(); $(element).append(e); })(true); 

6 Comments

I receive the error "Javescript Error: $ is not defined"
Doesn't work for me either. I'm getting the same error as Ben (I'm using JupyterLab).
It worked for me both in Jupyter and in JupyterLab. there may be some problem with your installation.
Works for me, but this also removes any other output generated in the cell.
Just look at the officially selected answer using warnings. This is unlikely to work.
|
5

For JupyterLab, this should work (@Alasja):

from IPython.display import HTML HTML('''<script> var code_show_err = false; var code_toggle_err = function() { var stderrNodes = document.querySelectorAll('[data-mime-type="application/vnd.jupyter.stderr"]') var stderr = Array.from(stderrNodes) if (code_show_err){ stderr.forEach(ele => ele.style.display = 'block'); } else { stderr.forEach(ele => ele.style.display = 'none'); } code_show_err = !code_show_err } document.addEventListener('DOMContentLoaded', code_toggle_err); </script> To toggle on/off output_stderr, click <a onclick="javascript:code_toggle_err()">here</a>.''') 

Comments

3

you need to specify the category otherwise even with the top solution above you'd get warnings

 warnings.filterwarnings("ignore", category=DeprecationWarning) 

Comments

2

TL;DR: It matters when you make a call to filterwarnings. Place warnings.filterwarnings(action="ignore") after all imports. In other words, do:

import warnings import third_party_module from a_cool_module import UsefulObject warnings.filterwarnings(action="ignore") # <--- ignore after imports # no other imports after this line 

Some explanation: Many libraries have code in their __init__.py file that prints warnings that the maintainers think you need to be aware of (warnings.filterwarnings("default", DeprecationWarning) or similar), so if you run warnings.filterwarnings("ignore") before importing any module (e.g. at the top of the notebook or even in the ipy_user_conf.py file) and you import objects, you end up overwriting your own filterwarnings call (that is supposed to suppress warnings) with the module's filterwarnings call (which prints warnings). This ends up printing a warning and you wonder why that happened. It is akin to the following:

import warnings warnings.filterwarnings(action="ignore") warnings.filterwarnings(action="default") warnings.warn("bla bla") # <--- warning printed 

What I'm suggesting is similar to the following:

import warnings warnings.filterwarnings(action="default") warnings.filterwarnings(action="ignore") warnings.warn("bla bla") # <--- warning is not shown 

This especially happens when you have already run a cell containing code to hide warnings in the beginning of a notebook but then import from a third-party library (e.g. pandas, matplotlib, scikit-learn etc.) in a cell further down in the notebook, which overwrites the previous filterwarnings to ignore warnings, and starts printing warnings.

2 Comments

I have like this but it not working
Which library and version are you using? I want to see why and update the answer if possible. Thanks
-1

The accepted answer does not work in some cases when the warnings are produced in child processes. In such cases, you can set the PYTHONWARNINGS environment variable to filter warnings from child processes.

os.environ["PYTHONWARNINGS"] = "ignore" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.