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
cwdandenv(merged withos.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 forcheckarg (default true).cwd(string or Path-like) - default forcwdarg (default empty).env(string dict) - merged withos.environas defaultenvarg.log_level(int) - level for command logging (defaultlogging.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"})
Metadata
Release files for ok-subprocess-runner 0.5
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| ok_subprocess_runner-0.5.tar.gz | 3.9 kB | Without provenance | Details for ok_subprocess_runner-0.5.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| ok_subprocess_runner-0.5-py3-none-any.whl | Python 3 | none | any | Details for ok_subprocess_runner-0.5-py3-none-any.whl |
Total release size: 8.8 kB
Release files / ok_subprocess_runner-0.5.tar.gz
| Download URL | ok_subprocess_runner-0.5.tar.gz |
|---|---|
| Size | 3.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
ac13323d5d948c11d56226da1f2df37c604eb0595a780ab18a81dccd552d642b
|
|
BLAKE2b-256 checksum How to use checksums |
7f7d9dba442ccecbf34f1ac53acb3ed840e235ff6fcfde4f52c3f0f60faae1d2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / ok_subprocess_runner-0.5-py3-none-any.whl
| Download URL | ok_subprocess_runner-0.5-py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
2691519e2a88c8d4b08f31cb5367caedb4918652820a6c1b4c363a569adc41a2
|
|
BLAKE2b-256 checksum How to use checksums |
41c28dd61a3423561f9cb6b7b2c44b8cc3ace368972c0ad2fb09c742256a02e8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Security for ok-subprocess-runner 0.5
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.