Python: suppressing warnings from Python applications

python-logoPython issues warning messages to users when an exception or halting the program is not warranted but there is still useful information that should be relayed to the end user.

Instead of following your first instincts to suppress these warnings you should instead resolve the root issue.  However, if you already understand the problem and (for example) need to keep end users from reporting a problem while the root issue is being addressed, then you can mute these warnings.

Simple warnings and muting

To give you an idea for how these warnings are created from inside Python code, here is a simple example.

python3 -c "import warnings; warnings.warn('hi'); print('ok')"

This prints a simple warning message, then goes on to complete the program by printing ‘ok’.

Muting specific warning

The PYTHONWARNINGS environment variable can be set to ignore just the messages that matched the string “hi”, which would allow warnings from other libraries and code but mute this specific warning.

PYTHONWARNINGS="ignore:hi" python3 -c "import warnings; warnings.warn('hi');print('ok')"

The syntax of the string is described in full in the documentation.  Not all fields need to be specified, as shown in our example above where we only use the “action:message” fields.

action:message:category:module:line

Muting all warnings

I would not recommend this because it could hide meaningful information, but if you wanted to mute all warnings from all categories, set the environment variable PYTHONWARNINGS to “ignore” without specifying any message or category.

PYTHONWARNINGS="ignore" python3 -c "import warnings; warnings.warn('hi');print('ok')"

Example

As an example, if you received the following error message from the gcloud CLI tool (which internally is using connectionpool.py)

/usr/bin/../lib/google-cloud-sdk/lib/third_party/urllib3/connectionpool.py:1043: InsecureRequestWarning: Unverified HTTPS request is being made to host 'gkehub.googleapis.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/1.26.x/advanced-usage.html#ssl-warnings

Then you could force it muted to your end-user community with the following value for PYTHONWARNINGS.

# mute by specific message text
export PYTHONWARNINGS="ignore:Unverified HTTPS request"

Note I have not had luck trying to use the category to mute the warning, so instead use the warning text as shown above.

# none of these worked, say 'unknown warning category'
export PYTHONWARNINGS="ignore::InsecureRequestWarning"
export PYTHONWARNINGS="ignore::SecurityWarning"
export PYTHONWARNINGS="ignore::HTTPWarning"

# this category does mute, but is so broad I would not use it
export PYTHONWARNINGS="ignore::Warning"

 

REFERENCES

python manual, warnings

stackoverflow, testing python warnings

NOTES

Force Python to throw error instead of just warn

PYTHONWARNINGS="error" python -c "import warnings; warnings.warn('hi');print('ok')"

Specification of PYTHONARNINGS

The PYTHONWARNINGS specification is colon separated and allows you to specify the action, message, and category.

action:message:category:module:line

Another example of muting warning

/usr/local/lib/python3.6/dist-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release.
from cryptography.exceptions import InvalidSignature

export PYTHONWARNINGS="ignore:Python 3.6 is no longer supported"