How to Ignore Deprecation Warnings in Python

How to ignore deprecation warnings in Python

From documentation of the warnings module:

 #!/usr/bin/env python -W ignore::DeprecationWarning

If you're on Windows: pass -W ignore::DeprecationWarning as an argument to Python. Better though to resolve the issue, by casting to int.

(Note that in Python 3.2, deprecation warnings are ignored by default.)

How to assuredly suppress a DeprecationWarning in Python?

Interesting enough, that even following @Alex's advice, I still have warnings output, like this:

import warnings
with warnings.catch_warnings():
warnings.simplefilter('ignore', category=DeprecationWarning)
from sklearn.externals.joblib import Parallel, delayed

def main():
xs = Parallel()(delayed(lambda x: x**2)(i) for i in range(1, 6))
print(sum(xs))

if __name__ == '__main__':
main()

# $ python -W ignore example.py
# [...]
# DeprecationWarning: the imp module is deprecated in favour of importlib;
# see the module's documentation for alternative uses
# import imp
# 55

So eventually, I decided to do it in a very hacky way and disable all warnings because I am bit tired of looking for a proper way to deal with them. (Not only for this library, but for many others that seem to be very eager about bombarding you with non-suppressible warnings).

import warnings
def noop(*args, **kargs): pass
warnings.warn = noop
from sklearn.externals.joblib import Parallel, delayed

def main():
xs = Parallel()(delayed(lambda x: x**2)(i) for i in range(1, 6))
print(sum(xs))

if __name__ == '__main__':
main()

If I use @Alex's advice wrongly or some of you have a better solution, I would be glad to accept it as an answer.


Update 1

Ok, it seems that it is pretty hard to influence the warnings, raised somewhere internally in the package. So probably the most simple thing would be to just replace warnings.warn with noop, or maybe somehow import the internal dependencies in advance and suppress them with a context manager.


Update 2

Some time ago I found one more possible way to deal with warnings. You can redirect them into logging. In case if no logger is explicitly configured, these warnings are essentially suppressed. It works for Jupyter and some libraries I've tested.

import logging
logging.captureWarnings(True)

How to disable Python warnings?

There's the -W option.

python -W ignore foo.py

setting `PYTHONWARNINGS` to disable python warnings seems to do nothing

PYTHONWARNINGS certainly does suppress python's warnings. Try running:

PYTHONWARNINGS="ignore" python -c "import warnings; warnings.warn('hi')"

But in this case you are not calling python, but openstack, which is apparently not inheriting the same environment. Without looking at the source I can't say why. It may even be explicitly settings the warning level, which will override anything you do before hand.

If you don't want to see errors, sending STDERR to /dev/null is the proper approach.

suppress deprecation in python

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

got the answer from https://stackoverflow.com/a/879249/15213571

Unable to suppress deprecation warnings

There are a couple of issues with the code you've tried. If you want to filter PendingDeprecationWarning, then you should use PendingDeprecationWarning in your code. Your code is using DeprecationWarning and RemovedInDjango110Warning, which are different warnings. Secondly, the fxn() function in the docs is an example function that creates a warning. It doesn't make sense to include it in your code.

You can either filter all pending deprecation warnings

import warnings
warnings.simplefilter("ignore", category=PendingDeprecationWarning)

However this might hide pending deprecations in your own code that you should fix. A better approach would be to use a context manager, to filter out warnings when importing the third-party lib.

with warnings.catch_warnings():
warnings.simplefilter("ignore", category=PendingDeprecationWarning)
from third_party_lib import some_module

How to suppress py.test internal deprecation warnings

From pytest --help:

--disable-pytest-warnings
disable warnings summary, overrides -r w flag


Related Topics



Leave a reply



Submit