Prefect Shell

0.3.5 · active · verified Sun Apr 12

Prefect Shell provides integrations for executing shell commands within Prefect flows. It allows users to embed shell scripts and commands directly into their data pipelines, leveraging Prefect's orchestration capabilities like logging, retries, and observability. This library is part of the Prefect ecosystem, currently at version 0.3.5, and is actively maintained with regular updates as part of the broader Prefect project.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to define a Prefect flow that executes shell commands using `ShellOperation`. It shows both the `.run()` method for simple, short commands and the context manager pattern (`with ShellOperation(...) as op: ...`) for long-running operations or when explicit output capture is required.

from prefect import flow
from prefect_shell import ShellOperation

@flow
def run_simple_command():
    # For short-running operations, use the .run() method
    result = ShellOperation(commands=["echo Hello from Prefect Shell!"]).run()
    print(f"Command output: {result.stdout.strip()}")

    # For longer-running operations or capturing structured output, use a context manager
    with ShellOperation(
        commands=["ls -l /tmp"],
        working_dir="/tmp"
    ) as list_files_operation:
        process = list_files_operation.trigger()
        process.wait_for_completion()
        output_lines = process.fetch_result()
        print("\nFiles in /tmp:")
        for line in output_lines:
            print(line.strip())

if __name__ == "__main__":
    run_simple_command()

view raw JSON →