1

I'm trying to config IPython show warning message once with module warnings, the simple code as below

import warnings
warnings.simplefilter('once', RuntimeWarning)
def x()
    warnings.warn('xxx', RuntimeWarning)
x()
C:\Program Files (x86)\Python37-32\Scripts\ipython:2: RuntimeWarning: xx
x()
C:\Program Files (x86)\Python37-32\Scripts\ipython:2: RuntimeWarning: xx

as you can see the warning always show, if I use warning.simplefilter('ignore', RuntimeWarning) then the warning message will be hide. and also tried in python terminal, it work as expected.

so is IPython doesn't support show warning message once? or is there anything wrong?

Thanks in advance.

dxaw2000
  • 11
  • 1
  • Do you run `x()` twice in two cells or `x();x()` once in one cell? – Vitalizzare Jun 14 '22 at 03:47
  • I ran x() twice – dxaw2000 Jun 14 '22 at 03:59
  • If you run `x();x()` in once cell, do you see one or two warnings? – Vitalizzare Jun 14 '22 at 04:02
  • if I use x();x() then will see one waring x();x() C:\Program Files (x86)\Python37-32\Scripts\ipython:2: RuntimeWarning: xxx but if I ran the command again still get the same waring message x();x() C:\Program Files (x86)\Python37-32\Scripts\ipython:2: RuntimeWarning: xxx according to the document, there should no warning output – dxaw2000 Jun 14 '22 at 04:24
  • Running a separate cell is like running a separate program. Running a pure python REPL is like executing one program step by step. That's why you see the warning each time you execute a cell but only once in REPL. – Vitalizzare Jun 14 '22 at 04:27
  • thanks Vitalizzare, is possible config ipython behavior like python REPL? – dxaw2000 Jun 14 '22 at 04:34
  • https://stackoverflow.com/a/9031848/14909621 – Vitalizzare Jun 14 '22 at 06:28
  • I tried this before and doesn't work, so I post this one – dxaw2000 Jun 14 '22 at 07:10

0 Answers0