如何忽略某些警告,而其他警告则转向来自 Python 命令行的异常?
How can certain warnings be ignored while others turned to exceptions from Python command line?
如何从命令行执行 Python 并忽略某些警告(例如 DeprecationWarning
),同时将其余警告变为异常?
可以多次使用 -W
选项吗?
我面临的具体问题是我 运行ning pytest
/pytest-cov
在 tox
/virtualenv
里面,因为 virtualenv
uses an outdated site.py
module,当我使用 运行 和 -Werror
时抛出 DeprecationWarning
,然后才能在我的代码中获取访问权限以捕获它。我想通过至少仅忽略此警告并将其他警告(ImportWarning
等)转换为异常来最大程度地减少对我的测试的影响,以便我可以在自己的代码中检测和处理它们。
python -Werror -Wignore::DeprecationWarning your_stuff.py
手册页中确实有这方面的信息,但有点难读。在发布问题之前我找不到这个问题,所以我会看看是否可以更容易找到它:
陷阱:-W
选项被后面的选项掩盖,-Werror
需要在 -Wignore
之前发生
按异常过滤:
python -werror -Wignore::PendingDeprecationWarning your_stuff.py
按行过滤:
python -Werror -Wignore::::32 your_stuff.py
忽略第 32 行的警告
过滤 module/file:
python -Werror -Wignore:::site: your_stuff.py
忽略示例中 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.
如何从命令行执行 Python 并忽略某些警告(例如 DeprecationWarning
),同时将其余警告变为异常?
可以多次使用 -W
选项吗?
我面临的具体问题是我 运行ning pytest
/pytest-cov
在 tox
/virtualenv
里面,因为 virtualenv
uses an outdated site.py
module,当我使用 运行 和 -Werror
时抛出 DeprecationWarning
,然后才能在我的代码中获取访问权限以捕获它。我想通过至少仅忽略此警告并将其他警告(ImportWarning
等)转换为异常来最大程度地减少对我的测试的影响,以便我可以在自己的代码中检测和处理它们。
python -Werror -Wignore::DeprecationWarning your_stuff.py
手册页中确实有这方面的信息,但有点难读。在发布问题之前我找不到这个问题,所以我会看看是否可以更容易找到它:
陷阱:-W
选项被后面的选项掩盖,-Werror
需要在 -Wignore
按异常过滤:
python -werror -Wignore::PendingDeprecationWarning your_stuff.py
按行过滤:
python -Werror -Wignore::::32 your_stuff.py
忽略第 32 行的警告
过滤 module/file:
python -Werror -Wignore:::site: your_stuff.py
忽略示例中 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.