subprocess-tee

0.4.2 · active · verified Thu Apr 09

This package provides a Pythonic alternative to `subprocess.run` that captures the output of a child process while simultaneously printing it to the console in real-time, mimicking the behavior of the `tee` command. It is designed for long-running processes where instant feedback is desirable. The current version is 0.4.2 and it maintains a stable release cadence.

Warnings

Install

Imports

Quickstart

The `run` function is designed to be a drop-in replacement for `subprocess.run`. By default, it prints output to `sys.stdout` and `sys.stderr` while also capturing it. Use `tee=False` to disable real-time printing if only output capture is needed. The `check=True` argument will raise a `CalledProcessError` on non-zero exit codes, similar to `subprocess.run`.

from subprocess_tee import run

# Basic usage: Command output is printed to console and captured
result = run(["echo", "Hello, subprocess-tee!"])
print(f"Captured stdout: {result.stdout.strip()}")
print(f"Return code: {result.returncode}")

# To explicitly disable tee functionality (e.g., just capture output):
result_no_tee = run(["python", "-c", "import time; print('start'); time.sleep(0.1); print('end')"], tee=False, capture_output=True, text=True)
print(f"Captured stdout without tee: {result_no_tee.stdout.strip()}")

# Example with error and checking return code:
try:
    run(["false"], check=True, capture_output=True, text=True)
except Exception as e:
    print(f"Command failed as expected: {e}")

view raw JSON →