0

I am working on a python library where we sometimes display user warnings using pythons warnings module. We now want to be able to turn off all of this warnings based on a flag passed to the python interpreter.

I read that the __init__ files are actually called first when importing a library but putting

import warnings
warnings.simplefilter('ignore')

in the highest level __init__.py does not change anything.

Are these options only for the current file? I am looking for a place where I can turn off all warnings in the library, because I don't want to manage this in every single file separately.

Simon
  • 194
  • 13
  • possible duplicate https://stackoverflow.com/questions/14463277/how-to-disable-python-warnings – nithin Oct 15 '19 at 13:52

2 Answers2

0

For Jupyter Notebook, you can try this:- Go to the '/home/user/.ipython/profile_default/startup/start.py' and load this module(If start.py is not present create one.)

Yash Kanojia
  • 154
  • 10
0

I'm not going to be surprised if this turns out to be horribly unpythonic, but for a similar issue (common formatting across modules) I have in __init__.py:

def warning_on_one_line(message, category, filename, lineno, file=None, line=None):
    return '%s:%s: %s: %s\n' % (filename, lineno, category.__name__, message)

warnings.formatwarning = warning_on_one_line

And then in submodules:

from package_name import warnings

That brings the changes made in __init__.py with it to all submodules.

David_O
  • 1,143
  • 7
  • 16
  • Thank you for the hint but I guess it is still rather annoying having to provide this in every submodule. – Simon Oct 21 '21 at 15:50