无法从‘信号’中导入名称‘SIGPIPE’

无法从‘信号’中导入名称‘SIGPIPE’

本文介绍了“无法从‘信号’中导入名称‘SIGPIPE’"在 Windows 10 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 lolcat 用于我的终端,但它抛出了 ImportError:

I am trying to use lolcat for my terminal, but it is throwing an ImportError:

$ lolcat
Traceback (most recent call last):
  File "C:/Users/eriku/Anaconda3/Scripts/lolcat", line 18, in <module>
    from signal import signal, SIGPIPE, SIG_DFL
ImportError: cannot import name 'SIGPIPE' from 'signal' (C:\users\eriku\anaconda3\lib\signal.py)

这指的导入 (signal.py) 是我机器上 Anaconda 安装的一部分.我也尝试在 Anaconda 提示符下执行 lolcat ,因为它在不同的终端上可能非常占主导地位,但这并没有帮助.有什么想法吗?

The import this refers to (signal.py) is part of the Anaconda install on my machine. I tried executing lolcat in Anaconda prompt as well because it can be very dominating when it comes to different terminals, but that didn't help. Any ideas?

推荐答案

检查 lolcat 的源代码表明这是 Python CLI 程序试图避免打印 IOError: [Errno32] 在 Linux 上接收 SIGPIPE 时断管,但忘记了 Windows.

Checking lolcat's source code shows that this is a typical problem of a Python CLI program trying to avoid printing IOError: [Errno 32] Broken pipe when receiving SIGPIPE on Linux, but forgetting about Windows.

我在我的代码中使用以下函数,而其他人可能更喜欢检查是否sys.platform == win32".

I use the following function in my code, while others may prefer to check if sys.platform == "win32".

def reset_sigpipe_handling():
    """Restore the default `SIGPIPE` handler on supporting platforms.

    Python's `signal` library traps the SIGPIPE signal and translates it
    into an IOError exception, forcing the caller to handle it explicitly.

    Simpler applications that would rather silently die can revert to the
    default handler. See https://stackoverflow.com/a/30091579/1026 for details.
    """
    try:
        from signal import signal, SIGPIPE, SIG_DFL
        signal(SIGPIPE, SIG_DFL)
    except ImportError:  # If SIGPIPE is not available (win32),
        pass             # we don't have to do anything to ignore it.

另请注意,官方文档建议改为处理异常:

不要将 SIGPIPE 的处置设置为 SIG_DFL 以避免 BrokenPipeError.这样做会导致您的程序在任何套接字连接中断而您的程序仍在写入时意外退出.

...但是该解决方案建议不完整(如果您需要在不重置 SIGPIPE 的情况下解决此问题,请参阅this处理程序).

...however the solution suggested there is incomplete (see this if you need to solve this without resetting the SIGPIPE handler).

最后,OSError: [Errno 22] Invalid argument"在带有 print() 和管道输出的 Windows 上 表明根本问题也存在于 Windows 上,但表现为 OSError: [Errno 22] Invalid argument" 而不是 BrokenPipeError.

Finally, "OSError: [Errno 22] Invalid argument" on Windows with print() and output piped indicates that the underlying problem exists on Windows as well, but manifests itself as an "OSError: [Errno 22] Invalid argument" instead of a BrokenPipeError.

这篇关于“无法从‘信号’中导入名称‘SIGPIPE’"在 Windows 10 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 02:37