How to disable python warnings?

前端 未结 8 1185
醉梦人生
醉梦人生 2020-11-22 04:24

I am working with code that throws a lot of (for me at the moment) useless warnings using the warnings library. Reading (/scanning) the documentation I only found a way to d

8条回答
  •  清歌不尽
    2020-11-22 04:41

    I realise this is only applicable to a niche of the situations, but within a numpy context I really like using np.errstate:

    np.sqrt(-1)
    
    __main__:1: RuntimeWarning: invalid value encountered in sqrt
    nan
    

    However, using np.errstate:

    with np.errstate(invalid='ignore'):
        np.sqrt(-1)
    
    nan
    

    The best part being you can apply this to very specific lines of code only.

提交回复
热议问题