Plumbum: Shell Combinators Library
Plumbum is a Python library for writing shell scripts in a Pythonic manner. It allows you to invoke external commands, chain them using pipes, redirect I/O, and build command-line applications programmatically. The current version is 1.10.0, and it maintains an active release cadence with minor versions and patch releases addressing bug fixes and Python version support.
Warnings
- breaking Plumbum has dropped support for older Python versions in recent releases. Version 1.10.0 requires Python >=3.9. Version 1.9.0 dropped support for Python <3.8, and v1.8.0 dropped Python 2.7 and 3.5.
- gotcha Complex or long-running `plumbum` pipelines can occasionally stall due to buffering or underlying OS pipe behavior. While fixes have been applied (e.g., v1.8.3), it remains a potential issue.
- gotcha Prior to v1.10.0, `plumbum.cli` applications might have incorrectly handled `False` values for flags, treating them as if the flag was not provided at all.
- breaking The packaging backend for `plumbum` moved from setuptools to Hatchling in v1.8.1. While this should be transparent for most users, third-party packaging or specific build systems might require adaptation.
Install
-
pip install plumbum
Imports
- cmd
from plumbum import cmd
- local
from plumbum import local
- SshMachine
from plumbum import SshMachine
- cli
from plumbum import cli
Quickstart
from plumbum import cmd
from plumbum.cmd import ls, grep
from plumbum import local
# Run a simple command and capture output
output = ls("-a")
print(f"ls -a output:\n{output}")
# Pipe commands together
pipe_output = (ls["-a"] | grep["-i", "py"])()
print(f"ls -a | grep -i py output:\n{pipe_output}")
# Access local machine paths and environment
print(f"Current directory: {local.cwd}")
print(f"User home: {local.home}")
# Execute a command in a specific directory
with local.cwd(local.tempdir):
temp_dir_files = ls()
print(f"Files in temporary directory: {temp_dir_files}")