contextlib2
Backports and enhancements for the contextlib module
What it is and what it does
contextlib2 is a backport of Python's standard library contextlib module, designed to make context manager utilities available on Python versions earlier than those in which they were added to the standard library. It includes both the core context manager decorators and utilities, plus type hints from the typeshed project.
Since the package requires Python 3.6 or later and the standard library has included contextlib since the earliest Python 3 releases, this package is most relevant for maintaining compatibility in legacy codebases. The package is no longer actively maintained, with its last release in 2021-06-27.
Use it for:
- Maintaining legacy code that must run on Python 3.6–3.10 while using a unified contextlib interface.
- Providing type hints for contextlib usage in projects that need static type checking across multiple Python versions.
- Serving as a reference implementation or testing ground for proposed enhancements to the standard library contextlib.
- Supporting projects that pin to older Python versions and need backported context manager utilities.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
contextlib2 backports the Python standard library's contextlib module to earlier Python versions, providing context managers and related utilities for resource management across Python 3.6 and later.
No, unless you are maintaining legacy code on Python 3.6–3.10 and need a specific backported feature not yet in your target Python version. For modern Python, use the standard library contextlib directly. The package is abandoned and receives no maintenance.
Install
contextlib2 on PyPI
pip
pip install contextlib2uv
uv add contextlib2poetry
poetry add contextlib2Installing contextlib2
Before you install
Low install friction with no runtime dependencies. However, the package is abandoned—last release was 1874 days ago. For modern Python versions, the standard library contextlib is available natively; this backport is primarily useful for legacy codebases or edge-case compatibility scenarios.
License in practice
Dual-licensed under the Python Software Foundation License (for the backported code) and Apache License 2.0 (for type hints from typeshed). Both are permissive; no restriction on commercial or proprietary use.
Quickstart
pip install contextlib2
import contextlib2
@contextlib2.contextmanager
def my_context():
print('enter')
yield
print('exit')
with my_context():
pass
Verify before relying
- Whether the backported APIs differ meaningfully from the current standard library contextlib.
- Whether Python 3.6, 3.7, 3.8, 3.9, or 3.10 versions targeted by this package remain in active use in your codebase.
Package facts
| License | PSF License (permissive) |
| Python support | supports the current Python release (>=3.6) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | abandoned — 1,874 days since the last release |
| First released | |
| Downloads | 12,264,081/month — #1,330 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: contextlib2-21.6.0-py2.py3-none-any.whl
Tags
More Software Development packages
Provides backported and experimental type hints…
permissive · top 100 on PyPI
numpyNumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
fastapiFastAPI is a Python web framework for building…
permissive · top 100 on PyPI
annotated-docProvides a way to document function parameters,…
permissive · top 100 on PyPI
typerTyper builds command-line applications from…
permissive · top 1,000 on PyPI
distlibDistlib provides low-level packaging utilities…
permissive · top 1,000 on PyPI
backports.tempfileProvides the TemporaryDirectory context manager…
permissive · top 5,000 on PyPI
pickle5Backports the pickle 5 protocol (PEP 574) and…
permissive · top 15,000 on PyPI
async_generatorProvides async generators and async context…
permissive · top 5,000 on PyPI
asyncioThis package is an obsolete backport of…
permissive · top 1,000 on PyPI
logutilsProvides additional logging handlers for…
permissive · top 15,000 on PyPI
linecache2Provides a backport of Python's linecache…
permissive · top 5,000 on PyPI
traceback2Provides a backport of Python's traceback…
permissive · top 5,000 on PyPI
textwrap3Backports Python 3.6's textwrap module to…
permissive · top 5,000 on PyPI
importlib-resourcesProvides a backport of Python's standard…
permissive · top 1,000 on PyPI
async-exit-stackProvides AsyncExitStack, a backport of Python…
permissive · top 15,000 on PyPI