Backports Entry Points Selectable
backports.entry_points_selectable is a compatibility shim that provides selectable entry points, specifically designed to ease the adoption of `importlib.metadata` 3.6+. It enables 'selectable' entry points (passing keyword arguments to `entry_points()` or invoking `.select()` on the result) even on older Python versions (prior to 3.10) or when `importlib_metadata` is older than 3.6. The current version is 1.3.0, and it receives updates as needed to maintain compatibility.
Warnings
- gotcha This backport is primarily for Python versions older than 3.10 or environments using `importlib_metadata` older than 3.6. For Python 3.10+ (which includes the selectable entry point interface in `importlib.metadata` directly), this library may be unnecessary and can be removed.
- deprecated The dictionary-like interface for `entry_points()` (e.g., `entry_points()['group_name']`) is deprecated in upstream `importlib.metadata` (Python 3.10+ and `importlib_metadata` 3.9+). While `backports.entry_points_selectable` aims to provide compatibility, relying on the dict interface is discouraged.
- gotcha This library only shims the `entry_points` function. If your project uses other APIs from `importlib.metadata` (e.g., `metadata`, `version`, `files`, `requires`), you might still need to explicitly depend on the `importlib_metadata` PyPI package if targeting Python versions older than 3.8.
Install
-
pip install backports-entry-points-selectable
Imports
- entry_points
from importlib.metadata import entry_points
from backports.entry_points_selectable import entry_points
Quickstart
from backports.entry_points_selectable import entry_points
# Example: Find all console scripts
console_scripts = entry_points(group='console_scripts')
print(f"Found {len(console_scripts)} console scripts.")
for ep in console_scripts:
print(f" - {ep.name}: {ep.value}")
# Example: Find a specific entry point by name and group
try:
(mypy_ep,) = entry_points(group='console_scripts', name='mypy')
print(f"\nFound specific entry point: {mypy_ep.name} -> {mypy_ep.value}")
# Load and execute the entry point (caution: this runs code)
# mypy_main = mypy_ep.load()
# mypy_main([])
except ValueError:
print("\n'mypy' console script not found or multiple found.")
# Fallback using .select() if no arguments were passed initially (less common with this backport)
all_entry_points = entry_points()
python_eps = all_entry_points.select(group='pytest11')
print(f"\nFound {len(python_eps)} pytest plugins.")
for ep in python_eps:
print(f" - {ep.name}: {ep.value}")