How to Disable Python Warnings?
Last Updated :
09 Apr, 2025
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.

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:

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:

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.

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.
Similar Reads
Perl | Warnings and how to handle them
Warning in Perl is the most commonly used Pragma in Perl programming and is used to catch 'unsafe code'. A pragma is a specific module in Perl package which has the control over some functions of the compile time or Run time behavior of Perl, which is strict or warnings. So the first line goes like
4 min read
Warnings in Python
Warnings are provided to warn the developer of situations that aren't necessarily exceptions. Usually, a warning occurs when there is some obsolete of certain programming elements, such as keyword, function or class, etc. A warning in a program is distinct from an error. Python program terminates im
5 min read
Python | Issue Warning Message
Problem - To have a program that can issue warning messages (e.g., about deprecated features or usage problems). Code #1 : Use the warnings.warn() function import warnings def func(x, y, logfile = None, debug = False): if logfile is not None: warnings.warn('logfile argument deprecated', DeprecationW
2 min read
Internal working of Python
Python is an object-oriented programming language like Java. Python is called an interpreted language. Python uses code modules that are interchangeable instead of a single long list of instructions that was standard for functional programming languages. The standard implementation of Python is call
5 min read
How to Change Warnings Display in MATLAB?
MATLAB displays warnings whenever there are errors in the execution of a code. These messages vary in detail depending on the method chosen by the user for their warning messages. MATLAB provides two modes of warning messages Verbose BacktraceIn verbose mode, MATLAB gives information on the error as
3 min read
How to write Comments in Python3?
Comments are text notes added to the program to provide explanatory information about the source code. They are used in a programming language to document the program and remind programmers of what tricky things they just did with the code and also help the later generation for understanding and mai
4 min read
How to Disable Output Buffering in Python
Output buffering is a mechanism used by the Python interpreter to collect and store output data before displaying it to the user. While buffering is often helpful for performance reasons there are situations where you might want to disable it to ensure that output is immediately displayed as it is g
3 min read
How to update Python on Windows?
In this article, we are going to see how to update Python in the Windows system. For the sake of example, we will be upgrading from Python 3.6.8 to Python 3.9.6. Here, we will see how to upgrade the Python version. Upgrading Python on WindowsTo check the current version of Python on your system, use
4 min read
How to use CMD for Python in Windows 10?
Using the Command Prompt (CMD) is an effective way to interact with Python on your Windows 10 machine. Whether you're writing scripts, testing code, or running programs, mastering CMD for Python in Windows 10 is crucial. This article will guide you on how to run Python in CMD, execute scripts, and t
4 min read
How to Learn Python Basics With ChatGPT
Python is one of the most popular programming languages, known for its simplicity and versatility. Whether you're a complete beginner or an experienced programmer looking to expand your skillset, mastering the basics of Python is essential. In this guide, we'll walk you through the fundamentals of P
4 min read