Subprocess Run

0.0.8 · maintenance · verified Fri Apr 17

subprocess-run is a Python library that extends the standard `subprocess` module, offering a simplified `run` function with opinionated defaults like `check=True` and stripped output. It provides a more convenient way to execute external commands compared to the standard library's `subprocess.Popen`. The current version is 0.0.8, with the last release approximately two years ago, indicating a maintenance-focused project rather than active development.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `subprocess_run.run` function for basic command execution, handling failing commands (due to default `check=True`), and providing input data via stdin using `input_data`.

from subprocess_run import run

# Execute a simple command
print("Executing a simple command:")
try:
    # run() returns (exit_code, stdout, stderr) by default with return_tuple=True
    exit_code, stdout, stderr = run(["echo", "Hello from subprocess-run!"])
    print(f"Success: '{stdout.strip()}'")
    print(f"Exit code: {exit_code}")
except Exception as e:
    print(f"Error: {e}")

# Command failing with default check=True
# 'false' is a common Unix command that always exits with a non-zero status.
# On Windows, you might use ['cmd', '/c', 'exit 1']
print("\nAttempting to run a command that will fail (check=True by default):")
try:
    run(["false"])
except Exception as e:
    print(f"Caught expected error for failing command: {e}") # e.g., CalledProcessError

# Command with input data (stdin)
# 'cat -' on Unix-like systems reads from stdin.
# For cross-platform, a simple python script could echo stdin:
# ['python', '-c', 'import sys; print(sys.stdin.read())']
print("\nExecuting a command with input data:")
try:
    input_text = "Data for stdin"
    _, output, _ = run(["cat", "-"], input_data=input_text.encode('utf-8'))
    print(f"Input: '{input_text}', Output from stdin command: '{output.strip()}'")
except Exception as e:
    print(f"Error with input_data: {e}")

view raw JSON →