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 Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright ยฉ Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap