Registry / workflow / prefect-shell

prefect-shell

JSON →
library0.3.6pypypi✓ verified 24d ago

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.

pip install "prefect[shell]"
INSTALL
IMPORT
SIG · PREFECT-SHELL
P
prefect-shell
workflowpythonv0.3.6
Install
21.0s avg
Import
5313ms
Disk
249MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.6 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 5.471s · 254.9MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 21.0s · import 5.156s · 255MB
249MB installed
● package 249MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

ShellOperation
from prefect_shell import ShellOperation
flow
from prefect import flow

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()
Debug
Known issues
breakingPrefect 2.x changed the core API from Prefect 1.x. `prefect-shell` is designed specifically for Prefect 2 and is not compatible with Prefect 1. Ensure your Prefect core installation is version 2.0 or higher.
fix
Upgrade your Prefect core installation to Prefect 2.x (e.g., `pip install -U prefect`) or ensure your environment uses Prefect 2.x if migrating from an older Prefect 1.x project.
affects: <2.0.0 of Prefect core
gotchaWhen using `ShellOperation`, calling `.run()` is suitable for short, synchronous commands. For long-running processes or when needing to manage the subprocess lifecycle (e.g., waiting for completion, capturing output), use the context manager pattern (`with ShellOperation(...)`) combined with `.trigger()` and `.wait_for_completion()`, followed by `.fetch_result()` to get output. Directly calling `.run()` inside a flow will execute synchronously and may block.
fix
For synchronous, short commands and immediate results: `result = ShellOperation(commands=['command']).run()`. For asynchronous execution, explicit process management, or long-running tasks: `with ShellOperation(...) as op: process = op.trigger(); process.wait_for_completion(); output = process.fetch_result()`.
affects: All versions
gotchaTo use `ShellOperation` as a Prefect block type (e.g., for creating and managing shell commands directly in the Prefect UI), you must explicitly register the blocks after installation.
fix
Run `prefect block register -m prefect_shell` in your terminal after installing the library to make its block types available in the Prefect UI.
affects: All versions
Errors
Common errors & fixes
ImportError: cannot import name 'ShellOperation' from 'prefect_shell.commands'
This error occurs when attempting to import 'ShellOperation' from an incorrect submodule (`prefect_shell.commands`) or when an outdated version of `prefect-shell` is installed. The `ShellOperation` class is intended to be imported directly from the top-level `prefect_shell` module.
fix
First, ensure `prefect-shell` is up-to-date by running `pip install -U prefect-shell`. Then, change the import statement to `from prefect_shell import ShellOperation`.
ModuleNotFoundError: No module named 'prefect_shell'
The `prefect-shell` library is not installed in the active Python environment, or the virtual environment where it was installed is not activated.
fix
Install the `prefect-shell` collection using pip: `pip install "prefect[shell]"`. Ensure you are installing it into and running your code from the correct Python environment.
RuntimeError: PID xxx failed with return code 1
This generic error indicates that the shell command executed by `ShellOperation` or `shell_run_command` itself failed and exited with a non-zero status code, signaling an execution problem within the command rather than a `prefect-shell` library issue.
fix
Debug the specific shell command(s) by running them directly outside of Prefect to identify the root cause of their failure (e.g., incorrect syntax, missing dependencies, insufficient permissions, or environment issues). For Prefect agents, ensure the agent process has the necessary permissions and access to files or directories referenced by the command.
NotImplementedError (when using shell_run_command)
This error can arise when using the older `shell_run_command` function, particularly in certain operating system environments (like Windows), where its underlying implementation might not be fully supported or has been superseded by newer functionality.
fix
Migrate from `shell_run_command` to the `ShellOperation` class, which is the recommended and more robust way to execute shell commands in Prefect flows. For example, replace `shell_run_command(command='...')` with `ShellOperation(commands=['...']).run()`.
Upgrade
Version history
0.3.6latest on PyPI · released Jun 5, 2026
Audit
Dependencies
prefectrequiredprefect-shell is an integration for the Prefect workflow orchestration framework and requires Prefect core to function.
Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
1
Resources
prefect-shell — pip install prefect-shell · libregistry