Runs
Runs is a Python library (version 1.3.0) that enhances the standard `subprocess` module by providing improved functions to execute blocks of text as sequences of shell commands. It adds features like multi-command execution, line continuations, comment handling, optional logging, error handling, lazy evaluation, and defaults to UTF-8 encoding. It offers more robust handling for scenarios that often trip up the native `subprocess` functions.
Warnings
- gotcha By default, the `runs` library splits input text into commands by newlines. Ensure that multi-line commands intended as a single unit use line continuations (e.g., `\`) as expected by your shell, or pass commands as a list of strings.
- gotcha While `runs` aims to improve `subprocess` error handling, raw shell commands executed with `shell=True` (which `runs` implies when passing a single string block) are susceptible to shell injection if untrusted input is included. Always sanitize or escape user-provided data.
- gotcha The `runs` library processes commands sequentially. For long-running or concurrent tasks, using `runs` might block your main program. It does not inherently provide asynchronous execution or process management capabilities beyond waiting for completion.
- breaking The library's functions (e.g., `run()`, `check_output()`) return a list of strings, one for each command executed in the input block. This differs from `subprocess.run()` which returns a single `CompletedProcess` object.
Install
-
pip install runs
Imports
- run
from runs import run
- call
from runs import call
- check_call
from runs import check_call
- check_output
from runs import check_output
Quickstart
from runs import run
# Run a block of text as multiple commands
output = run('''
echo "Hello from Runs!"
ls -l
''')
for line in output:
print(line)
# Run with error handling and echo
try:
result = run('false; echo ok', on_exception=False, echo=True)
except Exception as e:
print(f"Caught expected error: {e}")
# Get stdout of a single command
stdout_list = run('echo "Single command output"')
print(stdout_list[0])