Skip to content Skip to sidebar Skip to footer

"cannot Import Name 'SIGPIPE' From 'signal'" In Windows 10

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,

Solution 1:

It seems that SIGPIPE is not supported in Windows.

See the SIG* section on https://docs.python.org/3/library/signal.html#module-contents:

Note that not all systems define the same set of signal names; only those names defined by the system are defined by this module.


Solution 2:

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.

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.

Also note that the official docs recommend handling the exception instead:

Do not set SIGPIPE’s disposition to SIG_DFL in order to avoid BrokenPipeError. Doing that would cause your program to exit unexpectedly also whenever any socket connection is interrupted while your program is still writing to it.

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

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.


Post a Comment for ""cannot Import Name 'SIGPIPE' From 'signal'" In Windows 10"