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.4.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.4-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ok_subprocess_runner-0.4.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.4.tar.gz
Algorithm Hash digest
SHA256 858c373b61a6c4d8432dd6515c57a546839f40b55638d8abc0b49d9eceb6da27
MD5 35b0aa96619120b5b660f325e696163c
BLAKE2b-256 53076fe71c4516138c9f7e65245316dc8cc3fb2f3742472ac3d5cde8c09e959a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: ok_subprocess_runner-0.4-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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d59ed372c3c30fab1e9de8a9857eb0e8797ba8f2801b5b793b82bd89e597ee87
MD5 626f3a3ca41ed45e0c04fe6dd4abc42a
BLAKE2b-256 79bb14fc1d5c9242b00cee8130bc89386d98ce872f7034a9dff8bd04b2ead160

See more details on using hashes here.

Release history Release notifications | RSS feed

0.5

2 files

This release

0.4 This release

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