Open In App

How to Disable Python Warnings?

Last Updated : 09 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In Python, warnings are used to inform users of conditions that don’t necessarily require an exception but might indicate an underlying issue. However, sometimes these warnings can be unnecessary. There are two ways in which warnings can be ignored:

  • Disabling warnings from the code.
  • Disabling warnings with Command.

What are Python warnings?

Python warnings are messages that alert the user about potential issues in the program that don’t necessarily stop the execution or require exceptions to be raised. Warnings are typically not fatal to the program and execution continues, but they serve as notifications that something needs attention.

Example: The following is a warning that occurs when the path environment variable does not contain the path to the scripts folder of the Python distribution.

How to disable Python warnings?

The pip module was reinstalled, and a warning appeared during the process.

Again, the task at hand (reinstalling pip) was successfully completed, but the compiler warned about an irregularity detected in the paths. Regardless of whether the issue is resolved or not, it did not have a direct impact on the task. But this may not always be true.

Disabling warnings from the code

To disable warnings from within the code, we can use Python’s built-in warnings module. By setting up warning filters, we can prevent warnings from being displayed during the execution of the program.

Code with Warning Enabled (Default Behavior)

Python
import warnings

print('Hello')

# Displaying a warning message
warnings.warn('Error: A warning just appeared')

print('Geeks !')

Output:

How to disable Python warnings?

Warning Enabled

Explanation: In this example, a warning message is displayed using warnings.warn(). By default, Python has warnings enabled, so the message “Error: A warning just appeared” is shown in the output when the code is executed.

Code with Warnings Disabled

In the above code, a self-generated warning message was displayed. Since, by default, the program has warnings enabled, the message was displayed and the warning appeared. Now, the warnings are disabled, then an attempt to display the warning has been made:

Python
import warnings

print('Hello')

warnings.filterwarnings('ignore')

warnings.warn('Error: A warning just appeared')

print('Geeks !')

Output:

How to disable Python warnings?

Warnings Disabled

Explanation: In this version, warnings.filterwarnings(‘ignore’) is used to disable all warnings in the program. When warnings.warn() is called to generate a warning, no warning message is displayed because warnings have been filtered to be ignored.

Disabling Python Warnings with Command

In cases where the contents of the code cannot be modified to integrate warning suppression, warnings can be disabled externally before running the script. This is done by passing the ignore argument to the -W switch of the Python interpreter.

Syntax:

-W arg : warning control; arg is action:message:category:module:lineno
also PYTHONWARNINGS=arg

  • arg specifies the warning control options, such as the action to take, the message, category, module and line number.

By using the -W “ignore” string in the command, we can suppress warnings during the execution of the code. For example, to run a script without showing warnings, we can use the following command:

py -W “ignore” test.py

This command tells the Python interpreter to disable warnings before executing the test.py file.

How to disable Python warnings?

Disabling warnings with Command

To disable warnings during the execution of Python code files, use the following command syntax:

py -W “ignore” “_filename_”

In this example, _filename_ should be replaced with the name of our Python script. By passing -W “ignore”, the warnings will be ignored while executing the Python script.



Next Article
Practice Tags :

Similar Reads