How to disable warnings while Python file execution

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

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

Run the python file:
% python3
Hello there!

/Users/c2ctech/Desktop/ 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

python3 -Wignore

Read More:

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:

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