Registry / devops / openrewrite

openrewrite

JSON →
library8.84.9pypypi✓ verified 83d ago

OpenRewrite provides a powerful framework for automated code refactoring and static analysis. The `openrewrite` Python library offers Python bindings and a Command Line Interface (CLI) to leverage the core OpenRewrite engine, enabling programmatic application of recipes for code transformation in Python projects. It is currently at version 8.79.5 and follows the core OpenRewrite project's release cadence.

pip install openrewrite
INSTALL
IMPORT
SIG · OPENREWRITE
O
openrewrite
devopspythonv8.84.9
Install
4.9s avg
Import
Disk
39MB
Pass rate
5/ 10
Env Coverage5 / 10
glibc
3.93.13
musl
3.93.13
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
musl
py 3.103.910 runs
build_error
glibc
py 3.103.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
Debug
Known issues
breakingThe OpenRewrite Python library requires a Java 11 or newer Runtime Environment (JRE) to be installed and available on your system PATH. Without it, the underlying OpenRewrite CLI engine cannot execute.
fix
Install Java 11+ from Adoptium or your preferred vendor, and ensure the `java` executable is in your system PATH.
affects: All versions
breakingThe `openrewrite` Python library acts as a wrapper for the OpenRewrite CLI. This CLI must be separately installed and present in your system PATH for `rewrite_run` and other functions to operate.
fix
Install the OpenRewrite CLI (e.g., `brew install openrewrite/rewrite/rewrite` on macOS, or download from the official documentation) and verify it's in your system PATH by running `rewrite --version`.
affects: All versions
gotchaOpenRewrite operates on immutable Abstract Syntax Trees (ASTs). When developing recipes, you must always return new, transformed AST nodes rather than attempting to modify existing nodes in-place.
fix
Use transformation methods like `node.with_name(...)` or construct new nodes and always return the transformed `SourceFile` or `Tree` element from `visit_...` methods in your recipes.
affects: All versions
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.
Agent activity
8 hits · last 30 days
node
8
Resources
openrewrite — pip install openrewrite · libregistry