Registry / serialization / contextlib2

contextlib2

JSON →
library21.6.0pypypi✓ verified 27d ago

Contextlib2 provides backports and enhancements for Python's standard `contextlib` module, offering features like `ExitStack` and `asynccontextmanager` to older Python versions. The current version is 21.6.0, released in June 2021. The project appears to be in maintenance mode, as most of its key features are now integrated into the standard library for Python versions 3.7 and newer.

pip install contextlib2
INSTALL
IMPORT
SIG · CONTEXTLIB2
C
contextlib2
serializationpythonv21.6.0
Install
1.6s avg
Import
—
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
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
musl
py 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 17.8MB
glibc
py 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')
Debug
Known issues
gotchaFor Python 3.7 and newer, `contextlib2` is largely superseded. Most features it backports are available directly in the standard `contextlib` module. Importing from `contextlib2` in modern Python versions unnecessarily adds a dependency and might cause confusion.
fix
Prefer `from contextlib import ...` for `ExitStack`, `suppress`, `asynccontextmanager`, etc., in Python 3.7+ projects. Only use `contextlib2` if targeting older Python versions or very specific, documented enhancements.
affects: Python 3.7+
deprecatedThe project has seen minimal updates since its last release in June 2021. This indicates it is in a maintenance-only state, with no new features planned and potentially slow resolution for future bugs or security issues.
fix
Evaluate if the dependency is strictly necessary. If a feature is available in the standard library, prioritize that. Consider the long-term maintenance risk for new projects.
affects: All versions
gotchaDespite `contextlib2` requiring Python >=3.6, its primary utility was backporting features to significantly older Python versions (like 2.7 or early Python 3.x). For Python 3.6+, many of its features are already native, making its use redundant in many cases.
fix
Carefully review which specific features you intend to use. If they are already in the standard `contextlib` for your target Python 3.6+ version, remove the `contextlib2` dependency.
affects: Python 3.6+
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'contextlib2'
The 'contextlib2' package is not installed in your Python environment.
fix
pip 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.
fix
Replace `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.
fix
Ensure 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.

Agent activity
14 hits · last 30 days
node
12
Resources
contextlib2 — pip install contextlib2 · libregistry