pathspec is a utility library for gitignore-style pattern matching of file paths, implementing Git's wildmatch/gitignore specification. Current stable version is 1.0.4 (released 2025). The project is actively maintained by Caleb P. Burns and releases roughly a few times per year. It is a zero-dependency pure-Python library widely used by tools such as Black, pip, and Poetry for .gitignore-based file filtering.
pip install pathspecVerified import paths — ran on the pinned version, not inferred.
Compile gitignore-style patterns with PathSpec (documented semantics) or GitIgnoreSpec (true Git edge-case semantics), match individual paths or walk a directory tree.
Use spec.match_tree_files('path/to/dir', negate=True) to obtain files to keep, mirroring .gitignore exclusion logic.Use GitIgnoreSpec.from_lines() whenever you need to replicate what 'git status' / 'git ls-files' would show.
Replace PathSpec.from_lines('gitwildmatch', ...) with PathSpec.from_lines('gitignore', ...) and import from pathspec.patterns.gitignore.spec.GitIgnoreSpecPattern if you reference the class directly.Remove overrides of PathSpec._match_file(). Use the public match_file() / match_files() / match_tree_files() API or implement a custom backend instead.
Upgrade to >=0.10.1.
Always pass the root directory as the first argument and let the library normalise paths, or pre-normalise with os.path.relpath(). Do not feed absolute paths to match_file().
Install 'pip install google-re2', not 'pip install re2'.
Ensure a C++ compiler is installed in your environment before attempting to install `google-re2`. For Debian/Ubuntu-based systems, this typically means `apt-get install build-essential`. For RHEL/CentOS, `yum install gcc-c++` or `dnf install gcc-c++`.
Install a C++ compiler and build essentials in your environment. For Alpine Linux, this typically means running `apk add build-base` or `apk add g++`.
Install the library using pip: `pip install pathspec`
Update your import statement to use `from pathspec.patterns.gitignore.spec import GitIgnorePatternError` and adjust your code accordingly. If you need the exact prior behavior, refer to `GitIgnoreSpecPattern`.
For Git's behavior, use `from pathspec import GitIgnoreSpec` and then `GitIgnoreSpec.from_lines(...)`. For behavior aligning with `.gitignore` documentation, use `PathSpec.from_lines('gitignore', ...)`. If you explicitly need the exact behavior of the old `GitWildMatchPattern`, use `PathSpec.from_lines(GitIgnoreSpecPattern, ...)`.