简体   繁体   English

如何从python脚本中禁止特定警告?

[英]How to suppress specific warning from within python script?

I am getting a specific warning that I can reliably ignore. 我收到一个具体的警告,可以可靠地忽略它。 This warning is likely to be patched eventually, and my goal is to remove the warning from my console (to declutter it, less spam I have to look at). 该警告可能最终会被修补,并且我的目标是从控制台中删除该警告(以使其杂乱无章,我不得不查看的垃圾邮件也更少)。

Specifically, I am attempting to use FolderBrowse() in the Python3 package PySimpleGUI on macOS Mojave. 具体来说,我尝试在macOS Mojave上的Python3软件包PySimpleGUI使用FolderBrowse() This spits out the following message (on runtime): 这会在运行时发出以下消息:

objc[2542]: Class FIFinderSyncExtensionHost is implemented in both /System/Library/PrivateFrameworks/FinderKit.framework/Versions/A/FinderKit (0x7fff9408e3d8) and /System/Library/PrivateFrameworks/FileProvider.framework/OverrideBundles/FinderSyncCollaborationFileProviderOverride.bundle/Contents/MacOS/FinderSyncCollaborationFileProviderOverride (0x1073e4f50). One of the two will be used. Which one is undefined.

Again, my intent here is to ignore the above warning, not to fix it myself. 同样,我的目的是忽略上述警告,而不是自己解决。

I have seen other python warning suppression questions, such as this one 我看过其他python警告抑制问题,例如这个

However, I am not trying to hide a group of warning (eg: DeprecationWarning ). 但是,我并不是要隐藏一组警告(例如: DeprecationWarning )。 Instead I want to only hide the one warning shown above. 相反,我只想隐藏上面显示的一个警告。

edit, code I'm using: 编辑,我正在使用的代码:

import PySimpleGUI as sg
window_rows = [[sg.InputText(), sg.FolderBrowse()]]
sg.Window('', window_rows).Read()

I find that something like this works for this error: 我发现类似这样的东西可以解决这个错误:

copied_stderr = 0
try:
    if is_mac:
        # Redirect stderr to /dev/null to hide annoying FIFinderSyncExtensionHost warning
        copied_stderr = os.dup(2)
        devnull = os.open(os.devnull, os.O_WRONLY)
        os.dup2(devnull, 2)
        os.close(devnull)
    result = func(**args)
finally:
    if copied_stderr > 0:
        os.dup2(copied_stderr, 2)
        os.close(copied_stderr)

If you encounter this more than once, you can make a contextmanager that does the redirect. 如果您不止一次遇到这种情况,可以创建一个执行重定向的contextmanager。

See the section Temporarily Suppressing Warnings in the documentation of the warnings module: 请参阅warnings模块的文档中的暂时禁止警告部分:

If you are using code that you know will raise a warning, such as a deprecated function, but do not want to see the warning (even when warnings have been explicitly configured via the command line), then it is possible to suppress the warning using the catch_warnings context manager: 如果您正在使用知道会发出警告的代码(例如已弃用的函数),但又不想看到该警告(即使已通过命令行明确配置了警告),则可以使用来禁止显示该警告。 catch_warnings上下文管理器:

import warnings

def fxn():
    warnings.warn("deprecated", DeprecationWarning)

with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    fxn()

While within the context manager all warnings will simply be ignored. 在上下文管理器中,所有警告都将被忽略。 This allows you to use known-deprecated code without having to see the warning while not suppressing the warning for other code that might not be aware of its use of deprecated code. 这使您可以使用已知已弃用的代码,而不必查看警告,而不必为可能不知道其已弃用代码的其他代码取消警告。 (...) (...)

This seems exactly what you need, isn't it? 这似乎正是您所需要的,不是吗?

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

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