Buy Me a Coffee☕
*My post explains Warning with warn() in Python.
A warnings filter can set which warnings to show using these filters(actions) below. *"once" may not work properly:
Value | Disposition |
---|---|
"default" | print the first occurrence of matching warnings for each location (module line number) where the warning is issued |
"error" | turn matching warnings into exceptions |
"ignore" | never print matching warnings |
"always" | always print matching warnings |
"module" | print the first occurrence of matching warnings for each module where the warning is issued (regardless of line number) |
"once" | print only the first occurrence of matching warnings, regardless of location |
By default, these filters below are set to warnings.filters and they can be reset with resetwarnings() as shown below:
import warnings print(warnings.filters) # [('default', None,, '__main__', 0), # ('ignore', None, , None, 0), # ('ignore', None, , None, 0), # ('ignore', None, , None, 0), # ('ignore', None, , None, 0)] warnings.resetwarnings() print(warnings.filters) # []
Now, filterwarnings() or simplefilter() can filter warnings as shown below:
*Memos for filterwarnings():
*Memos for simplefilter():
my_project |-main.py |-file1.py(module) └-file2.py(module)
file1.py:
import warnings # Line 1 # Line 2 warnings.warn(message="Warning 1", category=UserWarning) # Line 3 warnings.warn(message="Warning 2", category=DeprecationWarning) # Line 4 warnings.warn(message="Warning 2", category=DeprecationWarning) # Line 5 warnings.warn(message="Warning 3", category=UserWarning) # Line 6
file2.py:
import warnings # Line 1 # Line 2 warnings.warn(message="Warning 1", category=UserWarning) # Line 3 warnings.warn(message="Warning 2", category=DeprecationWarning) # Line 4 warnings.warn(message="Warning 2", category=DeprecationWarning) # Line 5 warnings.warn(message="Warning 3", category=UserWarning) # Line 6
main.py with "default" filter(action):
import warnings warnings.resetwarnings() warnings.filterwarnings(action="default") warnings.filterwarnings(action="default", message="", category=Warning, module="", lineno=0, append=False) warnings.simplefilter(action="default") warnings.simplefilter(action="default", category=Warning, lineno=0, append=False) print(warnings.filters) # [('default', None,, None, 0)] import file1, file2 # ...\my_project\file1.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file1.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning) # ...\my_project\file2.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file2.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning)
main.py with "error" filter(action):
import warnings warnings.resetwarnings() warnings.filterwarnings(action="error") warnings.simplefilter(action="error") print(warnings.filters) # [('error', None,, None, 0)] import file1, file2 # ...\my_project\file1.py", line 3, in # warnings.warn(message="Warning 1", category=UserWarning) # UserWarning: Warning 1
main.py with "ignore" filter(action):
import warnings warnings.resetwarnings() warnings.filterwarnings(action="ignore") warnings.simplefilter(action="ignore") print(warnings.filters) # [('ignore', None,, None, 0)] import file1, file2 # Warnings are not shown
import warnings warnings.resetwarnings() warnings.filterwarnings(action="ignore", category=UserWarning) warnings.simplefilter(action="ignore", category=UserWarning) print(warnings.filters) # [('ignore', None,, None, 0)] import file1, file2 # ...\my_project\file1.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning)
import warnings warnings.resetwarnings() warnings.filterwarnings(action="ignore", message="Warning 1", category=UserWarning) print(warnings.filters) # [('ignore', re.compile('Warning 1', re.IGNORECASE), #, None, 0)] import file1, file2 # ...\my_project\file1.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning) # ...\my_project\file2.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning)
import warnings warnings.resetwarnings() warnings.filterwarnings(action="ignore", lineno=6) warnings.simplefilter(action="ignore", lineno=6) print(warnings.filters) # [('ignore', None,, None, 6)] import file1, file2 # ...\my_project\file1.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file1.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file2.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning)
main.py with "always" filter(action):
import warnings warnings.resetwarnings() warnings.filterwarnings(action="always") warnings.simplefilter(action="always") print(warnings.filters) # [('always', None,, None, 0)] import file1, file2 # ...\my_project\file1.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file1.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning) # ...\my_project\file2.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file2.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:5: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning)
main.py with "module" filter(action):
import warnings warnings.resetwarnings() warnings.filterwarnings(action="module") warnings.simplefilter(action="module") print(warnings.filters) # [('module', None,, None, 0)] import file1, file2 # ...\my_project\file1.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file1.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning) # ...\my_project\file2.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file2.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning)
main.py with "once" filter(action):
import warnings warnings.resetwarnings() warnings.filterwarnings(action="once") warnings.simplefilter(action="once") print(warnings.filters) # [('once', None,, None, 0)] import file1, file2 # ...\my_project\file1.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file1.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file1.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning) # ...\my_project\file2.py:3: UserWarning: Warning 1 # warnings.warn(message="Warning 1", category=UserWarning) # ...\my_project\file2.py:4: DeprecationWarning: Warning 2 # warnings.warn(message="Warning 2", category=DeprecationWarning) # ...\my_project\file2.py:6: UserWarning: Warning 3 # warnings.warn(message="Warning 3", category=UserWarning)
main.py with append argument. *If append=False, a filter is add before other filters while if append=True, a filter is add after other filters:
import warnings warnings.resetwarnings() print(warnings.filters) # [] warnings.filterwarnings(action="default", append=False) warnings.simplefilter(action="default", append=False) print(warnings.filters) # [('default', None,, None, 0)] warnings.filterwarnings(action="always", append=False) warnings.simplefilter(action="always", append=False) print(warnings.filters) # [('always', None, , None, 0), # ('default', None, , None, 0)] warnings.filterwarnings(action="once", append=True) warnings.simplefilter(action="once", append=True) print(warnings.filters) # [('always', None, , None, 0), # ('default', None, , None, 0), # ('once', None, , None, 0)]
부인 성명: 제공된 모든 리소스는 부분적으로 인터넷에서 가져온 것입니다. 귀하의 저작권이나 기타 권리 및 이익이 침해된 경우 자세한 이유를 설명하고 저작권 또는 권리 및 이익에 대한 증거를 제공한 후 이메일([email protected])로 보내주십시오. 최대한 빨리 처리해 드리겠습니다.
Copyright© 2022 湘ICP备2022001581号-3