subprocess-tee
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
- gotcha The `subprocess-tee.run` function implies `text=True` (or `universal_newlines=True`) by default. This means output is treated as text (decoded using default encoding, usually UTF-8), which differs from `subprocess.run` where `text=False` (binary output) is the default unless specified.
- gotcha There are known open issues on Windows related to incorrect argument list conversion and lack of support for multiple arguments when not using `shell=True`. This can lead to commands not executing as expected.
- gotcha The library may not correctly handle non-UTF-8 characters in the output of child processes due to an open bug. This could lead to `UnicodeDecodeError` or garbled output.
- gotcha Using `shell=True` with user-provided input in commands can introduce security vulnerabilities (e.g., command injection). While this is a general `subprocess` module concern, it applies equally to `subprocess-tee`.
Install
-
pip install subprocess-tee
Imports
- run
from subprocess_tee import run
Quickstart
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}")