Install & Compatibility
Where this runs
tested against v8.84.9 · 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
muslpy 3.10–3.910 runs
build_error
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 4.9s · import 0.000s · 23MB
39MB installed
● package 39MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Recipe
✓ from rewrite import Recipe
✗ from openrewrite.recipe import Recipe
This quickstart demonstrates how to define a basic OpenRewrite Python recipe and execute it using `rewrite_run`. It creates a temporary Python file, applies a no-op recipe in dry-run mode, and then cleans up. Critical prerequisites are a Java 11+ Runtime Environment and the OpenRewrite CLI, which the Python library orchestrates.
import os
import pathlib
from openrewrite.recipe import Recipe
from openrewrite.run import rewrite_run
from openrewrite.tree import SourceFile
# Create a dummy Python file for the recipe to process
dummy_file_path = pathlib.Path("example_for_rewrite.py")
with open(dummy_file_path, "w") as f:
f.write("print('Hello, OpenRewrite!')\n")
f.write("import os # This import will be processed by a real recipe\n")
class MyPythonRecipe(Recipe):
def get_display_name(self) -> str:
return "My First Python Recipe"
def get_description(self) -> str:
return "A simple recipe that does nothing, demonstrating the API."
def visit_source_file(self, source_file: SourceFile, execution_context) -> SourceFile:
# In a real recipe, you would inspect and modify the source_file here.
# For this example, we just print the path and return it unchanged.
print(f"[Recipe] Visiting file: {source_file.get_source_path()}")
return source_file
# --- IMPORTANT: Ensure OpenRewrite CLI and Java 11+ are installed and in PATH ---
# The `rewrite_run` function calls the `rewrite` CLI tool internally.
# If you encounter FileNotFoundError or Java-related errors, check your setup.
try:
print(f"Running recipe on {dummy_file_path} (dry_run=True)...")
results = rewrite_run(
[MyPythonRecipe()],
[str(dummy_file_path)], # List of file paths to process
dry_run=True, # Set to False to apply changes directly to files
# For more complex scenarios, you might use an InMemoryExecutionContext:
# execution_context=InMemoryExecutionContext()
)
print("Recipe run complete.")
if not results:
print("No changes detected (as expected for this basic recipe in dry_run mode).")
else:
print("Changes detected (this might indicate an issue with the dummy recipe or environment setup if unexpected).")
except Exception as e:
print(f"An error occurred during rewrite_run. Please ensure OpenRewrite CLI and Java 11+ are installed and in PATH.\nError: {e}")
finally:
# Clean up the dummy file
if dummy_file_path.exists():
os.remove(dummy_file_path)
print(f"Cleaned up {dummy_file_path}.")
openrewrite --version
Upgrade
Version history
8.84.9latest on PyPI · released Jun 11, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.10 or higher.
java-runtimerequiredRequires Java 11+ runtime for the underlying OpenRewrite CLI engine to function.
openrewrite-clirequiredThe Python library wraps the OpenRewrite CLI, which must be installed and available in the system PATH.