traceback from a warning

后端 未结 1 1951
南方客
南方客 2021-01-01 18:06

I have a code which, at some point shows a warning, I think that it is having a problem calculating a mean()

I would like to know if there is any way to

相关标签:
1条回答
  • 2021-01-01 18:32

    You can turn warnings into exceptions:

    import warnings
    
    warnings.simplefilter("error")
    

    Now instead of printing a warning, an exception will be raised, giving you a traceback.

    You can get the same effect with the -W command line switch:

    $ python -W error somescript.py
    

    or by setting the PYTHONWARNINGS environment variable:

    $ export PYTHONWARNINGS=error
    

    You can play with the other warnings.simplefilter() arguments to be more specific about what warning should raise an exception. You could filter on warnings.RuntimeWarning and a line number, for example.

    0 讨论(0)
提交回复
热议问题