Install & Compatibility
Where this runs
tested against v21.6.0 · 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 · 17.8MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.6s · import 0.000s · 18MB
16MB installed
● package 16MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
ExitStack
✓ from contextlib2 import ExitStack
✗ from contextlib import ExitStack
While `ExitStack` is in `contextlib` from Python 3.3+, importing from `contextlib2` might be done for consistency in mixed-version environments or for older Python 3 versions where `contextlib2` provided fixes/enhancements.
contextmanager
✓ from contextlib2 import contextmanager
`contextmanager` is standard, but `contextlib2` could be used for very old Python 3 versions or specific backport needs.
suppress
✓ from contextlib2 import suppress
`suppress` is in standard `contextlib` from Python 3.4+. Use `contextlib2` only if targeting Python <3.4 or specific behavior.
redirect_stdout
✓ from contextlib2 import redirect_stdout
`redirect_stdout` is in standard `contextlib` from Python 3.4+. Use `contextlib2` only if targeting Python <3.4 or specific behavior.
asynccontextmanager
✓ from contextlib2 import asynccontextmanager
✗ from contextlib import asynccontextmanager
`asynccontextmanager` is in standard `contextlib` from Python 3.7+. If targeting Python <3.7, `contextlib2` was a backport solution. For Python 3.7+, use the standard library.
This example demonstrates `ExitStack` to manage multiple context managers dynamically. It opens several files and ensures they are all properly closed upon exiting the `with` block, even if errors occur.
from contextlib2 import ExitStack
import os
def open_files(filenames):
with ExitStack() as stack:
files = [stack.enter_context(open(fname, 'r')) for fname in filenames]
yield files
# Example usage (will create dummy files first)
if __name__ == '__main__':
# Create dummy files
with open('file1.txt', 'w') as f: f.write('Hello from file 1')
with open('file2.txt', 'w') as f: f.write('Hello from file 2')
try:
with open_files(['file1.txt', 'file2.txt']) as opened:
for i, f in enumerate(opened):
print(f"Content of file {i+1}: {f.read()}")
finally:
os.remove('file1.txt')
os.remove('file2.txt')
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'contextlib2'
The 'contextlib2' package is not installed in your Python environment.
fixpip install contextlib2
AttributeError: module 'contextlib' has no attribute 'nested'
The `contextlib.nested` function was deprecated in Python 3 and removed in Python 3.9. 'contextlib2' does not backport this deprecated feature, but rather newer enhancements.
fixReplace `contextlib.nested` with `contextlib2.ExitStack` or the standard library's `contextlib.ExitStack` (available in Python 3.3+).
AttributeError: 'Foo' object has no attribute '__exit__'
`ExitStack.enter_context()` expects to manage an object that adheres to the context manager protocol (i.e., has `__enter__` and `__exit__` methods). This error occurs when the provided object lacks these methods.
fixEnsure the object passed to `ExitStack.enter_context()` is a valid context manager. If the object only provides a `close()` method, wrap it with `contextlib2.closing()` (e.g., `stack.enter_context(closing(my_object))`).
Upgrade
Version history
21.6.0latest on PyPI · released Jun 27, 2021
Audit
Dependencies
No dependency data recorded yet.