Wurlitzer

3.1.1 · active · verified Sat Apr 11

Wurlitzer is a Python library that captures C-level `stdout` and `stderr` streams, making it possible to integrate output from underlying C code into Python's I/O system. This is particularly useful in environments like Jupyter notebooks or when calling native libraries that print directly to the console. As of April 2026, the current stable version is 3.1.1, and it maintains a steady release cadence, with recent updates focusing on stability and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `wurlitzer.pipes` to capture C-level `stdout` and `stderr` into Python `StringIO` objects, and `wurlitzer.sys_pipes` to forward C-level output directly to Python's `sys.stdout` and `sys.stderr` streams. It uses `ctypes` to simulate C functions printing to the console.

import ctypes
import io
from wurlitzer import pipes, STDOUT

# Simulate a C function that prints to stdout/stderr
libc = ctypes.CDLL(None) # Load standard C library
def c_printf(msg):
    libc.printf(b'%s\n', msg.encode('utf8'))
def c_fprintf_stderr(msg):
    # Find stderr pointer, differs slightly by OS
    try:
        c_stderr_p = ctypes.c_void_p.in_dll(libc, 'stderr')
    except ValueError:
        c_stderr_p = ctypes.c_void_p.in_dll(libc, '__stderrp')
    libc.fprintf(c_stderr_p, b'%s\n', msg.encode('utf8'))

# Example 1: Capture stdout and stderr separately
out_buf = io.StringIO()
err_buf = io.StringIO()
with pipes(stdout=out_buf, stderr=err_buf):
    c_printf('Hello from C stdout!')
    c_fprintf_stderr('Hello from C stderr!')

print(f"Captured STDOUT: '{out_buf.getvalue().strip()}'")
print(f"Captured STDERR: '{err_buf.getvalue().strip()}'")

# Example 2: Forward C-level stdout/stderr to Python's sys.stdout/stderr
# which might already be redirected (e.g., in a Jupyter notebook)
from wurlitzer import sys_pipes
print("\n--- Using sys_pipes (output below is from Python's streams) ---")
with sys_pipes():
    c_printf('C stdout via sys_pipes!')
    c_fprintf_stderr('C stderr via sys_pipes!')
print("--- End sys_pipes example ---")

view raw JSON →