How to Disable Python Warnings

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.

Hide all warnings in ipython

I eventually figured it out. Place:

import warnings
warnings.filterwarnings('ignore')

inside ~/.ipython/profile_default/startup/disable-warnings.py. I'm leaving this question and answer for the record in case anyone else comes across the same issue.

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

warnings.filterwarnings(action='once')

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.)



Related Topics



Leave a reply



Submit