How to get rid of specific warning messages in python while keeping all other warnings as normal?

If Scipy is using the warnings module, then you can suppress specific warnings. Try this at the beginning of your program:

import warnings
warnings.filterwarnings("ignore", message="divide by zero encountered in divide")

If you want this to apply to only one section of code, then use the warnings context manager:

import warnings
with warnings.catch_warnings():
    warnings.filterwarnings("ignore", message="divide by zero encountered in divide")
    # .. your divide-by-zero code ..

Leave a Comment