How to disable warnings while Python file execution

Python will by default prints warning messages to sys.stderr.

warning_example.py
import warnings

def print_warning():
    print("Hello there!")
    warnings.warn("This is a warning message!", Warning)

print_warning()
Run the python file:
% python3 warning_example.py
Warning:
Hello there!

/Users/c2ctech/Desktop/warning_example.py:5: Warning: This is a warning message!
  warnings.warn("This is a warning message!", Warning)

If you wish to not display this warning message when you execute the Python script, all you need to do is pass in the option -Wignore

Example:
python3 -Wignore warning_example.py

Read More:

https://docs.python.org/3/using/cmdline.html#cmdoption-W

Warning Control Description
-Wdefault Warn once per call location
-Werror Convert to exceptions
-Wmodule Warn once per calling module
-Walways Warn every time
-Wonce Warn once per Python process
-Wignore Never warn

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!