python中忽略警告的命令,当其他警告从Python命令行转向异常时,如何可以忽略某些警告?...

手册页上确实有这方面的信息,但有点难以阅读。在发布问题之前,我无法找到此问题,因此我将查看是否可以更容易地找到:

陷阱:-W选项被后面的选项屏蔽,-Werror需要在之前出现-Wignore

按异常筛选:python -werror -Wignore::PendingDeprecationWarning your_stuff.py

按行筛选:

^{pr2}$

因为忽略了第32行的警告

按模块/文件筛选:

^{3}$

忽略来自示例中site.py模块的任何警告(异常回溯中的最后一个文件)

选项是action:message:category:module:line

手册页:-W argument

Warning control. Python sometimes prints warning message to sys.stderr. A typical warning message

has the following form: file:line: category: message. By default, each warning is printed once for

each source line where it occurs. This option controls how often warnings are printed. Multiple -W

options may be given; when a warning matches more than one option, the action for the last matching

option is performed. Invalid -W options are ignored (a warning message is printed about invalid

options when the first warning is issued). Warnings can also be controlled from within a Python

program using the warnings module.

The simplest form of argument is one of the following action strings (or a unique abbreviation):

ignore to ignore all warnings; default to explicitly request the default behavior (printing each

warning once per source line); all to print a warning each time it occurs (this may generate many

messages if a warning is triggered repeatedly for the same source line, such as inside a loop); modâ€

ule to print each warning only the first time it occurs in each module; once to print each warning

only the first time it occurs in the program; or error to raise an exception instead of printing a

warning message.

The full form of argument is action:message:category:module:line. Here, action is as explained

above but only applies to messages that match the remaining fields. Empty fields match all values;

trailing empty fields may be omitted. The message field matches the start of the warning message

printed; this match is case-insensitive. The category field matches the warning category. This

must be a class name; the match test whether the actual warning category of the message is a subâ€

class of the specified warning category. The full class name must be given. The module field

matches the (fully-qualified) module name; this match is case-sensitive. The line field matches the

line number, where zero matches all line numbers and is thus equivalent to an omitted line number.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值