Skip to main content

ok-subprocess-runner for Python

Trivial wrapper for Python subprocess.run with defaults and logging.

You probably won't want to use this. Just call subprocess.run directly (it's perfectly lovely), write your own trivial helper, or use one of these libraries:

  • sh - call any shell command as if it were a function
  • Plumbum - shell-like syntax for Python
  • zxpy - ~ string operator to run shell commands
  • shellpy - ``` string operator to run shell commands
  • shell - another wrapper for subprocess
  • pipepy - pipe operators and function wrappers for shell commands
  • python-shell - another shell command runner

But, this is my wrapper, and it does these things:

  • Checks command return (check=True) by default
  • Uses explicit argument vectors (shell=False) by default
  • Includes easy-peasy methods to capture stdout as text or lines
  • Logs all commands run, escaped for cut-and-paste rerunning
  • Lets you set defaults for cwd and env (merged with os.environ)
  • Converts Path-like arguments to strings
  • Passes extra keyword arguments through to subprocess.run

Collectively, this is what I want for subprocesses -- tweaks to subprocess.run (or subprocess.check_call) to make it super easy to never write shell scripts again. Your mileage will almost certainly vary!

Usage

Add this package as a dependency:

  • pip install ok-subprocess-runner
  • OR just copy the ok_subprocess_runner/ module (it has no dependencies)

Import the module, create an ok_subprocess_runner.SubprocessRunner object, and call it to run commands:

import logging
import ok_subprocess_runner
...
sub = ok_subprocess_runner.SubprocessRunner()
...
logging.basicConfig(level=logging.INFO)  # to show the logging
...
sub("echo", "Hello World!")

Command arguments are individual function arguments; otherwise, usage is identical to subprocess.run including keyword arguments and return value.

The logging output looks like this:

$ python test.py
INFO:root:🐚 echo 'Hello World!'
Hello World!

Note that arguments are escaped so you can cut-and-paste the command.

Configuring defaults

SubprocessRunner objects have properties that set defaults:

  • .args_prefix (list of string or Path-like) - prepended to all commands run
  • .check (bool) - default for check arg (default true)
  • .cwd (string or Path-like) - default for cwd arg (default empty)
  • .env (string dict) - merged with os.environ as default env arg
  • .log_level (int) - level for command logging (default logging.INFO)

Capturing output

SubprocessRunner objects have some utility wrappers to capture output:

  • .stdout_text(args, ...) - returns captured stdout as a text string
  • .stdout_lines(args, ...) - returns captured stdout split into lines

Pass-through

All calls pass keyword arguments through to subprocess.run.

sub = ok_subprocess_runner.SubprocessRunner()
sub("echo", "Hello World!", check=False, cwd="/tmp", env={"FOO": "BAR"})

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ok_subprocess_runner-0.5.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ok_subprocess_runner-0.5-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file ok_subprocess_runner-0.5.tar.gz.

File metadata

  • Download URL: ok_subprocess_runner-0.5.tar.gz
  • Upload date:
  • Size: 3.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ok_subprocess_runner-0.5.tar.gz
Algorithm Hash digest
SHA256 ac13323d5d948c11d56226da1f2df37c604eb0595a780ab18a81dccd552d642b
MD5 f1c62e04c7fc7797d942c9a084d721c2
BLAKE2b-256 7f7d9dba442ccecbf34f1ac53acb3ed840e235ff6fcfde4f52c3f0f60faae1d2

See more details on using hashes here.

File details

Details for the file ok_subprocess_runner-0.5-py3-none-any.whl.

File metadata

  • Download URL: ok_subprocess_runner-0.5-py3-none-any.whl
  • Upload date:
  • Size: 4.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.19 {"installer":{"name":"uv","version":"0.11.19","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for ok_subprocess_runner-0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2691519e2a88c8d4b08f31cb5367caedb4918652820a6c1b4c363a569adc41a2
MD5 24f7afee801240400fc1d82540f32208
BLAKE2b-256 41c28dd61a3423561f9cb6b7b2c44b8cc3ace368972c0ad2fb09c742256a02e8

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.5 This release

2 files

0.4

2 files

0.3

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page