patch-ng
Library to parse and apply unified diffs.
What it is and what it does
Patch NG is a maintained fork of the original python-patch library, designed to parse and apply unified diff patches to files. It operates as a self-contained Python module with no external dependencies, making it suitable for embedding directly in projects or installing via pip. The library automatically corrects common diff issues—linefeeds mismatched to the target file, diffs corrupted by whitespace stripping, and a/ and b/ prefixes—and detects patch format (SVN, HG, GIT). It runs on Python 3.6 and later across Linux, Windows, and macOS.
The fork was created to address stalled maintenance in the original project, accepting pull requests that had been pending review since mid-2019. It works as both a command-line tool and a library, offering a diffstat histogram for inspection. The package does not handle file renaming, creation, removal, directory tree operations, or version-control-specific metadata; it is limited to applying unified diff formats to existing files.
Use it for:
- Automatically apply code patches or configuration diffs during build or deployment pipelines.
- Parse and inspect unified diffs programmatically to extract or validate patch metadata.
- Correct malformed patches by automatically fixing whitespace and linefeed inconsistencies.
- Embed patch application logic in tools without introducing external dependencies.
- Generate and display diffstat histograms for patch review or reporting.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Parses and applies unified diff patches to files, with automatic correction of linefeeds, whitespace issues, and diff prefixes.
Yes. The package is actively maintained, has no external dependencies, and solves a specific, well-defined problem—parsing and applying unified diffs with automatic correction of common issues. It is stable (Production/Stable status), widely used (top 5000 PyPI packages), carries no known vulnerabilities, and uses a permissive MIT license. Install it if you need to apply patches programmatically or embed patch logic without dependency overhead.
Install
patch-ng on PyPI
pip
pip install patch-nguv
uv add patch-ngpoetry
poetry add patch-ngInstalling patch-ng
Before you install
Low friction: pure Python with no external dependencies, distributed as a single wheel. Actively maintained with recent commits; last release 113 days ago.
License in practice
MIT license permits unrestricted use, modification, and distribution in commercial and private projects with minimal obligations.
Quickstart
pip install patch-ng
from patch_ng import PatchSet
with open('diff.patch') as f:
patches = PatchSet(f)
patches.apply()
Requires Python 3.6 or later; file renaming, creation, removal, and directory operations are not supported.
Verify before relying
- Exact behavior when applying patches to files with mixed line endings or encoding issues.
- Performance characteristics on large patches or files.
- Whether the package handles edge cases in malformed diffs beyond the documented automatic corrections.
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.6) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 113 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 4,710,651/month — #2,250 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: patch_ng-1.19.1-py3-none-any.whl
Keywords: patch, parse, diff, strip, diffstat
Tags
More Build Tools packages
Provides reusable utilities for Python…
permissive · top 100 on PyPI
tqdmWraps any iterable to display a real-time…
copyleft · top 100 on PyPI
pippip is the standard installer for Python…
permissive · top 100 on PyPI
hatchlingHatchling is a standards-compliant Python build…
permissive · top 100 on PyPI
grpcio-toolsGenerates Python gRPC service stubs and message…
permissive · top 1,000 on PyPI
pre-commitpre-commit is a framework for installing and…
permissive · top 1,000 on PyPI
patchParses and applies unified diff patches to…
permissive · top 15,000 on PyPI
moreorlessGenerates unified diffs with correct handling…
permissive · top 5,000 on PyPI
whatthepatchParses and applies patch files in multiple diff…
permissive · top 5,000 on PyPI
patchyPatchy modifies Python functions at runtime by…
permissive · top 5,000 on PyPI
diff-match-patchProvides diff, match, and patch algorithms for…
permissive · top 5,000 on PyPI
unidiffParses unified diff format data into structured…
permissive · top 1,000 on PyPI
unidiff2Parses unified diff format into structured…
permissive · top 15,000 on PyPI
fast-diff-match-patchWraps the C++ implementation of…
permissive · top 15,000 on PyPI
xmldiffxmldiff compares two XML files or trees and…
permissive · top 15,000 on PyPI
jsonpatchApplies JSON Patches according to RFC 6902,…
permissive · top 1,000 on PyPI