简体   繁体   English

无法抑制在导入调用期间执行的警告

[英]Cannot suppress warnings that are executed during an import call

I'm unable to filter warnings in Python when they're emitted as part of an import.当它们作为导入的一部分发出时,我无法过滤 Python 中的警告。 I've tested the following with Python3.8.我已经用 Python3.8 测试了以下内容。

This works:这有效:

File A:文件A:

import warnings

def old_func():
    warnings.warn("This is an old function.")
    pass

File B:文件乙:

import warnings
from file_a import old_func

warnings.simplefilter("ignore")
old_func()

The warning message is suppressed as expected.警告消息按预期被抑制。 But if I change File A like this:但是,如果我像这样更改文件 A:

import warnings

def old_func():
    warnings.warn("This is an old function.")
    pass

old_func()

It stops working.它停止工作。 I assumed that was because I wasn't setting the filter before the import so I changed File B as follows:我认为这是因为我在导入之前没有设置过滤器,所以我按如下方式更改了文件 B:

import warnings
warnings.simplefilter("ignore")
from file_a import old_func
old_func()

And still no dice.仍然没有骰子。 The warning is emitted no matter what I do.无论我做什么,都会发出警告。 I've also tried the catch_warnings() context manager as follows:我还尝试了 catch_warnings() 上下文管理器,如下所示:

import warnings
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    from file_a import old_func
    old_func()

The warning is still emitted.警告仍然发出。 If the warning is emitted during an import there seems to be no way of ignoring it.如果在导入期间发出警告,似乎无法忽略它。

I've done some hacky things like temporarily making stderr and stdout in-memory file-like-objects temporarily and that works but it's terrible.我已经做了一些hacky 的事情,比如临时制作stderr 和stdout 内存中的类似文件的对象,这可行,但很糟糕。 I'd like to find a way to do this with reasonable machinery.我想找到一种方法来用合理的机器来做到这一点。

Since this is for a command line tool with an entry-point created with setuptools, it's quite a pain to package it in a way that adds a -W option to the interpreter for end users.由于这是针对具有使用 setuptools 创建的入口点的命令行工具,因此以向最终用户的解释器添加 -W 选项的方式对其进行打包是一件非常痛苦的事情。

A side update: I've confirmed this works on Python 3.6 and does not work on Python 3.8.附带更新:我已经确认这适用于 Python 3.6,不适用于 Python 3.8。

Thanks for the minimal reproducible examples !感谢您提供最少的可重复示例!

I followed your examples, running them each time.我跟着你的例子,每次都运行它们。 But I did not observe the same thing than you :但我没有观察到与你相同的事情:

# file: a.py
import warnings

def old_func():
    warnings.warn("This is an old function.")
    pass

old_func()
# file: b.py
import warnings
warnings.simplefilter("ignore")
from a import old_func
old_func()

does not emit the warning for me, on Windows CPython 3.6.8 .在 Windows CPython 3.6.8上不会为我发出警告。 Can you check again ?你能再检查一下吗?

I assumed that was because I wasn't setting the filter before the import so I changed File B as follows looks very reasonable to me我认为那是因为我在导入之前没有设置过滤器,所以我更改了文件 B,如下所示对我来说非常合理

The last version of your A file works too (ie the warning is not displayed)您的 A 文件的最后一个版本也有效(即不显示警告)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM