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
-

Facing issues? Have Questions? Post them here! I am happy to answer!


Author: Rakesh
Author Info:

Rakesh is a seasoned developer with over 10 years of experience in web and app development, and a deep knowledge of operating systems. Author of insightful How-To articles for Code2care.

Follow him on: X

Copyright © Code2care 2023 | Privacy Policy | About Us | Contact Us | Sitemap