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

I'm currently running the openstack executable and it generates python deprecation warnings.

After some searching I did find this howto.

The relevant part is here:

Use the PYTHONWARNINGS Environment Variable to Suppress Warnings in Python

We can export a new environment variable in Python 2.7 and up. We can export PYTHONWARNINGS and set it to ignore to suppress the warnings raised in the Python program.

However, doing this:

PYTHONWARNINGS="ignore" openstack image show image name -f value -c id

does nothing, deprecation warnings are still displayed.

I've tried setting PYTHONWARNINGS to various things:

  • ignore
  • "ignore"
  • "all"
  • "deprecated"
  • "ignore::DeprecationWarning"
  • "error::Warning,default::Warning:has_deprecated_syntax"
  • "error::Warning"

but none of them seem to do anything.

I was able to work around the issue by appending 2>/dev/null to the end but I would like to know why PYTHONWARNINGS doesn't seem to do anything.


Solution 1:

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.