satoru
satoru

Reputation: 33225

Ignoring python warnings

I want to ignore all the UserWarning in my dev environment so that they are not printed into my error log file.

I've read the documentation of warnings module, and tried something like:

import warnings
import the_module_that_warns

warnings.simplefilter("ignore", UserWarning)

But UserWarning still get printed, why's that?

Upvotes: 21

Views: 30118

Answers (1)

glglgl
glglgl

Reputation: 91017

If the modules warns on it import, the way you do it is too late.

Instead, do

import warnings
warnings.simplefilter("ignore", UserWarning)

import the_module_that_warns

in order to tell the warnings module what to ignore before the warning comes.

Upvotes: 39

Related Questions