Wrapper for subprocess.run() with defaults and logging
Project description
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"})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ok_subprocess_runner-0.3.tar.gz.
File metadata
- Download URL: ok_subprocess_runner-0.3.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08a06071b2128318ba9121e8d5bca91cfd5ec01c3ead2fc8cc42f5be25d7ab47
|
|
| MD5 |
89ed9c5910e82f52d303a8a25fa95634
|
|
| BLAKE2b-256 |
d46e8296c2d79434d02e0a8f762b7f4c138998a193d251b8c4000c925584c2fc
|
File details
Details for the file ok_subprocess_runner-0.3-py3-none-any.whl.
File metadata
- Download URL: ok_subprocess_runner-0.3-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.11 {"installer":{"name":"uv","version":"0.11.11","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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af194ef76a01b3a43da4427bac387c5991cb02909e82ddbaa6cbc1ed3c7c0620
|
|
| MD5 |
746699a105aab0753106fa33d842ee1d
|
|
| BLAKE2b-256 |
64a4f58058eab25805556377d322122e80073043c8b1bc3844efb7d26b2c0a3b
|