Registry / data / patch-ng

patch-ng

JSON →
library1.19.1pypypi✓ verified 24d ago

Library to parse and apply unified diffs. `patch-ng` is a fork of the original `python-patch` project, providing active maintenance and bug fixes. The current version is 1.19.0, released in October 2025. It is actively maintained by Conan-io with regular releases and is compatible with Python 3.6+.

pip install patch-ng
INSTALL
IMPORT
SIG · PATCH-NG
P
patch-ng
datapythonv1.19.1
Install
1.5s avg
Import
99ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v1.19.1 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.104s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.094s · 18MB
16MB installed
● package 16MB
Code
Verified usage

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

PatchSet
from patch_ng import PatchSet
from patch_ng import from_string, from_file, PatchSet
fromfile
from patch_ng import fromfile
from patch_ng import from_file
Hunk
from patch_ng import Hunk

This example demonstrates how to create a simple text file, define a unified diff patch as a string, parse it using `from_string`, and then apply the patch to the file within a temporary directory using `patchset.apply()`.

import os import tempfile import shutil from patch_ng import from_string, PatchSet # Create a temporary directory to work in temp_dir = tempfile.mkdtemp() original_file_path = os.path.join(temp_dir, "my_file.txt") try: # 1. Create an original file with open(original_file_path, "w") as f: f.write("Line 1: Hello world\n") f.write("Line 2: This is a test.\n") f.write("Line 3: End of file.\n") print(f"Original file '{os.path.basename(original_file_path)}' created in {temp_dir}:\n" \ f"{open(original_file_path, 'r').read()}") # 2. Define a unified diff patch string # This patch changes 'Line 2' and adds 'New Line 2.5' patch_content = """--- a/my_file.txt +++ b/my_file.txt @@ -1,3 +1,4 @@ Line 1: Hello world -Line 2: This is a test. +Line 2: This line was changed. +New Line 2.5: Inserted line. Line 3: End of file. """ # 3. Parse the patch string # The `strip` argument removes leading path components (e.g., 'a/' or 'b/') patchset = from_string(patch_content) if not patchset: print("Failed to parse patch content.") else: # 4. Apply the patch to the specified root directory # The `root` argument is crucial to apply the patch outside the current working directory. success = patchset.apply(strip=1, root=temp_dir) if success: print(f"Patch applied successfully to files in {temp_dir}.") print(f"\nPatched file content of '{os.path.basename(original_file_path)}':\n" \ f"{open(original_file_path, 'r').read()}") else: print("Failed to apply patch.") finally: # Clean up the temporary directory shutil.rmtree(temp_dir) print(f"\nCleaned up temporary directory: {temp_dir}")
Debug
Known issues
breakingThe library was renamed from `python-patch` to `patch-ng` at version 1.17. Users migrating from the old library must update their `pip install` command and `import` statements.
fix
Update installation command to `pip install patch-ng` and change import statements (e.g., `import patch` to `from patch_ng import ...`).
affects: >=1.17
breakingAs of version 1.18.0, `patch-ng` requires Python 3.6 or newer. Older Python versions are no longer supported.
fix
Upgrade your Python environment to 3.6 or later.
affects: >=1.18.0
gotchaPrior to version 1.19.0, `patch-ng` had known issues with applying Git patches involving file renames/moves, changes in file permissions, incorrect handling of Git full index format, and general issues with large patch files or quoted file paths. These issues could lead to incomplete or incorrect patch application.
fix
Upgrade to `patch-ng` version 1.19.0 or later to ensure correct and robust patch application in these scenarios.
affects: <1.19.0
Upgrade
Version history
1.19.1latest on PyPI · released Apr 23, 2026
Audit
Dependencies
pythonrequiredRequires Python 3.6 or higher for compatibility.
Agent activity
9 hits · last 30 days
node
6
Resources
patch-ng — pip install patch-ng · libregistry