How to reset warnings completely How to reset warnings completely numpy numpy

How to reset warnings completely


How can I see the warning again without restarting python?

As long as you do the following at the beginning of your script, you will not need to restart.

import pandas as pdimport numpy as npimport warningsnp.seterr(all='warn')warnings.simplefilter("always")

At this point every time you attempt to divide by zero, it will display

RuntimeWarning: divide by zero encountered in true_divide 

Explanation:

We are setting up a couple warning filters. The first (np.seterr) is telling NumPy how it should handle warnings. I have set it to show warnings on all, but if you are only interested in seeing the Divide by zero warnings, change the parameter from all to divide.

Next we change how we want the warnings module to always display warnings. We do this by setting up a warning filter.

What is the difference between first and second warning? Why only the second one is stored in this __warningregistry__? Where is the first one stored?

This is described in the bug report reporting this issue:

If you didn't raise the warning before using the simple filter, this would have worked. The undesired behavior is because of __warningsregistry__. It is set the first time the warning is emitted. When the second warning comes through, the filter isn't even looked at. I think the best way to fix this is to invalidate __warningsregistry__ when a filter is used. It would probably be best to store warnings data in a global then instead of on the module, so it is easy to invalidate.

Incidentally, the bug has been closed as fixed for versions 3.4 and 3.5.


warnings is a pretty awesome standard library module. You're going to enjoy getting to know it :)

A little background

The default behavior of warnings is to only show a particular warning, coming from a particular line, on its first occurrence. For instance, the following code will result in two warnings shown to the user:

import numpy as np# 10 warnings, but only the first copy will be shownfor i in range(10):    np.true_divide(1, 0)# This is on a separate line from the other "copies", so its warning will shownp.true_divide(1, 0)

You have a few options to change this behavior.

Option 1: Reset the warnings registry

when you want python to "forget" what warnings you've seen before, you can use resetwarnings:

# warns every time, because the warnings registry has been resetfor i in range(10):    warnings.resetwarnings()    np.true_divide(1, 0)

Note that this also resets any warning configuration changes you've made. Which brings me to...

Option 2: Change the warnings configuration

The warnings module documentation covers this in greater detail, but one straightforward option is just to use a simplefilter to change that default behavior.

import warningsimport numpy as np# Show all warningswarnings.simplefilter('always')for i in range(10):    # Now this will warn every loop    np.true_divide(1, 0)

Since this is a global configuration change, it has global effects which you'll likely want to avoid (all warnings anywhere in your application will show every time). A less drastic option is to use the context manager:

with warnings.catch_warnings():    warnings.simplefilter('always')    for i in range(10):        # This will warn every loop        np.true_divide(1, 0)# Back to normal behavior: only warn oncefor i in range(10):    np.true_divide(1, 0)

There are also more granular options for changing the configuration on specific types of warnings. For that, check out the docs.