Search code examples
pythonexceptionerror-handlingruntime-errorsuppress-warnings

How do I suppress a warning in the IDE in Python? (IDE is Jupyter)


Is there a simple way to hide an error in the IDE printout?

logts = np.log(ts)
plt.plot(logts, amps, "1", ms=10)

Is my relevant section of code. Due to ts containing a 0 as its first point, Python isn't overly fond of the np.log(ts) operation. The IDE (Jupyter) throws an error message

C:\ProgramData\Anaconda3\lib\site-packages\ipykernel_launcher.py:119: RuntimeWarning: divide by zero encountered in log

But the rest of the code runs fine, the relevant pair of results (the ts = 0, and respective amps value are removed later anyway when plotting best fit lines to stop problems with infinities. But I would still like to supress that specific error message, since I know what's causing it and it's otherwise fine.


Solution

  • You can use

    import warnings; 
    warnings.simplefilter('ignore')
    

    at the start of your program to ignore all warnings.


    If you want to ignore only that specific warning then use "seterr"

    np.seterr(divide = 'ignore') 
    logts = np.log(ts)
    np.seterr(divide = 'warn')