Install & Compatibility
Where this runs
tested against v0.7 · 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.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 19.2MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 2.4s · import 0.000s · 20MB
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
glob2
✓ import glob2
The primary way to import the library and access its functions like `glob2.glob()` and `glob2.iglob()`.
This quickstart demonstrates `glob2`'s main features: recursive globbing using `**` to find all header files within a directory and its subdirectories, and capturing parts of filenames using `with_matches=True`.
import glob2
import os
# Create some dummy files and directories for demonstration
os.makedirs('src/media/mp3', exist_ok=True)
with open('src/fs.h', 'w') as f: f.write('// fs.h')
with open('src/media/mp3.h', 'w') as f: f.write('// mp3.h')
with open('src/media/mp3/frame.h', 'w') as f: f.write('// frame.h')
with open('src/main.py', 'w') as f: f.write('# main.py')
# Example 1: Recursive globbing with '**'
print('All header files:')
all_header_files = glob2.glob('src/**/*.h')
for f in all_header_files:
print(f)
# Example 2: Capturing patterns (with_matches=True)
# This example requires a pattern that defines capture groups, e.g., project-(.*).zip
# For demonstration, let's assume we have files like 'binaries/project-v1.0.zip'
os.makedirs('binaries', exist_ok=True)
with open('binaries/project-v1.0.zip', 'w') as f: f.write('zip content')
with open('binaries/project-v2.1.zip', 'w') as f: f.write('zip content')
print('\nProject versions (captured patterns):')
for filename, (version,) in glob2.iglob('./binaries/project-*.zip', with_matches=True):
print(f'{filename}, version: {version}')
# Clean up dummy files and directories
import shutil
shutil.rmtree('src', ignore_errors=True)
shutil.rmtree('binaries', ignore_errors=True)
Debug
Known issues
deprecatedThe primary features of `glob2`, specifically recursive globbing with `**`, have been integrated into Python's standard `glob` module since Python 3.5. `pathlib` also provides robust globbing functionality. There is generally no compelling reason to use `glob2` in modern Python versions.fixPrefer `glob.glob(..., recursive=True)` from the standard library or `Path.glob()` and `Path.rglob()` from `pathlib`.
affects: < Python 3.5 for 'standard glob' limitations, all versions for 'glob2' redundancy
breakingThe `glob2` library is no longer actively maintained. The last release was in 2019, and the last code push was over 4 years ago. This means it may not be compatible with newer Python versions or address new security vulnerabilities or bugs.fixMigrate to `glob` or `pathlib` from Python's standard library for continued support and maintenance.
affects: 0.7 and earlier
gotchaThe recursive wildcard `**` in `glob2` (and standard `glob`) must appear as a standalone directory element. For example, `src/**/*.h` is correct, but `src/**h` will not have the desired recursive effect. Additionally, `**/*.py` will match Python files in the current directory (`.`), if not intended, use `*/**/*.py` to exclude the current directory.fixEnsure `**` is isolated between path separators (e.g., `dir1/**/dir3/*.txt`). Adjust patterns (e.g., `*/**/*.py`) if you want to exclude immediate children of the starting directory when using `**`.
affects: All versions of glob2
gotchaWhen using Windows file paths, backslashes are interpreted as escape characters. This can lead to incorrect path matching or errors.fixUse raw strings (e.g., `r'C:\Users\...'`) or forward slashes (e.g., `'C:/Users/...'`) for path patterns on Windows to avoid issues with backslash escapes.
affects: All Python versions on Windows
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'glob2'
The `glob2` library is not installed in the Python environment being used.
fixInstall the library using pip: `pip install glob2` (or `pip3 install glob2` for Python 3 installations).
ImportError: No module named 'glob2'
The `glob2` library is not found in the Python environment, often due to not being installed or being installed for a different Python version.
fixEnsure `glob2` is installed for your active Python environment: `pip install glob2` (or `pip3 install glob2`).
NameError: name 'glob2' is not defined
The `glob2` module was not imported before being used, or an attempt was made to call `glob2.glob` without explicitly importing `glob2` first.
fixAdd `import glob2` at the top of your Python script before using `glob2.glob()`.
TypeError: glob() got an unexpected keyword argument 'recursive'
This error typically occurs when attempting to use the `recursive=True` argument with Python's built-in `glob.glob()` function in a Python version older than 3.5. The `glob2` library was created to provide this functionality before it was standardized.
fixFor Python versions < 3.5, use `glob2.glob('**/*.txt', recursive=True)` after `import glob2`. For Python 3.5 and above, use the standard `glob.glob('**/*.txt', recursive=True)`. Upgrade
Version history
0.7latest on PyPI · released Jun 10, 2019
Audit
Dependencies
No dependency data recorded yet.