If you are a python programmer or have worked with coding on Python, you definitely would have faced warnings and errors when compiling or running the code. Therefore in this article, we are going to discuss How to suppress warnings in Python.
In some cases, when you want to suppress or ignore warnings in Python, you need to use some specific filter function of the warnings module. We will discuss the usage of those functions in this article. Thus, you can learn to ignore or suppress warnings when needed.
Warnings And Its Types
A warning in Python is a message that programmer issues when it is necessary to alert a user of some condition in their code. Although this condition normally doesn’t lead to raising an exception and terminating the program. Let’s understand the types of warnings.
The table given above shows different warning classes and their description.
|Base category for warnings related to bytes and bytearray.
|Base category for warnings about deprecated features when those warnings are intended for other Python developers (ignored by default, unless triggered by code in main).
|Base category for warnings about deprecated features when those warnings are intended for end users of applications written in Python.
|Base category for warnings triggered during the process of importing a module (ignored by default).
|Base category for warnings about features that will be deprecated in the future (ignored by default).
|Base category for warnings related to resource usage (ignored by default).
|Base category for warnings about dubious runtime features.
|Base category for warnings about dubious syntactic features.
|Base category for warnings related to Unicode.
|The default category for warn().
|This is the base class of all warning category classes. It is a subclass of Exception.
Suppress All Warnings In Python
Just like everything in Python is an object, similar warnings are also objects in Python. You can program them too. You have to use the ‘warnings’ package to ignore warnings. Firstly we will see how you can ignore all warnings in python step by step:
- Import ‘warnings’ module
- Use the ‘filterwarnings()’ function to ignore all warnings by setting ‘ignore’ as a parameter.
warnings.filterwarnings('ignore') # setting ignore as a parameter
Suppress Specific Warnings In Python
Further, let’s see how to suppress specific warnings in Python using the warnings package. For stopping particular signs, we will need to add another parameter in the ‘filterwarnings()’ function, i.e., category.
- import warnings
- Use the ‘filterwarnings()’ function to ignore all warnings by setting ‘ignore’ as a parameter. In addition to that, add a parameter ‘category’ and specify the type of warning.
warnings.filterwarnings(action='ignore', category=FutureWarning) # setting ignore as a parameter and further adding category
Similarly, you can add any category you desire and suppress those warnings.
Suppressing Pandas warnings
You can even suppress pandas warnings in order to do that. You have to write a code to suppress warnings before importing pandas.
warnings.simplefilter(action='ignore', category=FutureWarning) # setting ignore as a parameter and further adding category
Suppressing Warnings In Tensorflow
Further, you can even ignore tensorflow warnings if you want. The way to ignore warnings in tensorflow is a bit different. Let’s understand step by step:
- For TF 2.x, you can use the following code
- For TF 1.x, you can use the following code
The codes mentioned above are used to remove logging information. Therefore any messages will not be printed. Further, if you want to remove deprecated warnings or future warnings in TF 1. x, you can use:
from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
To suppress futurewarnings along with current deprecated warnings, use:
Suppress Warnings in Python IDE (Pycharm)
When you use an IDE like Pycharm, you can disable inspections, so no warnings are raised. Moreover, you can also suppress a warning for a particular line of code.
- Disable warnings for a particular line of code.
from application import routes # noqa
By commenting ‘noqa,’ you can suppress warnings for that single line of code. In addition to that, if you want to suppress all warnings, you can follow these given steps:
- Go to Settings dialog (Ctrl+Alt+S) and select Editor/Inspections.
- And then go to the inspection you want to disable, further uncheck the checkbox next to it.
- Apply the changes and close the dialog box.
Suppress Pylint Warnings
To disable pylint warnings, you can also use the symbolic identities of warnings rather than memorize all the code numbers, for example:
# pylint: disable=locally-disabled, multiple-statements, fixme, line-too-long
You can use this comment to disable any warnings for that line, and it will apply to the code that is coming after it. Similarly, it can be used after an end of a line for which it is meant.
Disable Warnings In Jupyter Notebook
You can suppress all warnings in the jupyter notebook by using the warnings module and using functions like ‘simplefilter()’ and ‘filterwarnings()’.
Further, to suppress warnings for a particular line of codes, you can use :
warning_function() #now warnings will be suppressed
Disable Warning While Ansible Execution
You can disable all the warnings when using ansible by making the deprecation_warnings = ‘false’ in defaults section of your effective configuration file i.e.(/etc/ansible/ansible.cfg, ~/.ansible.cfg).
Suppress Matplotlib Warnings
To suppress the matplotlib library, first import all required modules in addition to that import warnings module. Further use the’ filterwarnings()’ function to disable the warnings.
import numpy as np
from matplotlib import pyplot as plt
Then finish writing your remaining code, you will see no warnings pop up, and the code will be executed.
Disable SSL Warnings Python Requests
Further, let’s see how you can disable security certificate checks for requests in Python.
When we use the requests module, we pass ‘verify = False’ along with the URL, which disables the security checks.
# sending a get http request to specified link URL
response = requests.request(
"GET", "https://www.yoururlhere.com", verify=False)
Bypassing the ‘verify=False,’ you can make the program execute without errors.
FAQs on Suppress Warnings Python
You can use the ‘filterwarnings()’ function from the warnings module to ignore warnings in Python.
You can use the syntax ‘np.seterr(all=”ignore”)’ to ignore all warnings.
You can use the ‘filterwarnings()’ function from the warnings module and set ‘default’ as a parameter to re-enable warnings.
In this article, we have seen how we can suppress warnings when needed, although warnings are essential as they can signify a problem you might leave unseen. Therefore it is advised to code with warnings enabled. Only disable them when it is of utmost importance to ignore them.
To learn something new and exciting, check out this post.
Reference for “Table 1.1” is official python documentation (python 3.10.6 documentation) https://docs.python.org/3/library/warnings.html