Beginner Guide to Disable or Ignore Python warnings – Python Tutorial

By | December 3, 2021

When we are running python script, we may get some warnings. Here is an example:

How to disable Python warnings

How to disable or ignore these warnings? In this tutorial, we will introduce you how to do.

How to disable or ignore python warnings?

Here are some methods than can do it. They are:

Method 1: Use -W ignore argument, here is an example:

python -W ignore file.py

Method 2: Use warnings packages

import warnings
warnings.filterwarnings("ignore")

This method will ignore all warnings.

Method 3: Ignore part of warnings in python script

We will use with warnings.catch_warnings() to ignore warnings in some python code in a python file.

import warnings
warnings.filterwarnings("ignore")

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    #your python code

Leave a Reply