skillfed

contextlib2

Backports and enhancements for the contextlib module

contextlib2 v21.6.0 12.3M downloads/30d#1,330 on PyPI
Permissive license PSF License Abandoned released

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 contextlib2

uv

uv add contextlib2

poetry

poetry add contextlib2

Installing 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

Development Status :: 5 - Production/StableLicense :: OSI Approved :: Apache Software LicenseLicense :: OSI Approved :: Python Software Foundation LicenseProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.6Programming Language :: Python :: 3.7Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

context manager backportcontextlib for older pythonresource management utilitiespython standard library backportcontext manager enhancementscontextlib compatibilityasync context manager support
backportcontext-managerslegacy-support

More Software Development packages