pola-rs/polars · error · ImportError

gevent is required for using…

Error message

gevent is required for using LazyFrame.collect_async(gevent=True) orpolars.collect_all_async(gevent=True)

What it means

Creating an AsyncDataFrame/collect callback wrapper with gevent=True requires the gevent package. When gevent is not importable, the wrapper's __init__ raises ImportError explaining that LazyFrame.collect_async(gevent=True) and polars.collect_all_async(gevent=True) need it.

Solutions

  1. Install gevent: `pip install gevent` (or `pip install 'polars[async]'` if the extra is offered)
  2. Switch to gevent=False to use the default asyncio-based flow if gevent integration is not required
  3. Add gevent to your requirements/lockfile so deployment images include it
  4. Confirm the interpreter running the code is the one where gevent was installed

Example fix

// before
out = lf.collect_async(gevent=True)  # ImportError: gevent is required
// after
# pip install gevent
out = lf.collect_async(gevent=True)
Defensive patterns

Strategy: validation

Validate before calling

import importlib.util
if importlib.util.find_spec('gevent') is None:
    raise SystemExit("gevent required: pip install gevent")

Type guard

def gevent_available() -> bool:
    import importlib.util
    return importlib.util.find_spec('gevent') is not None

Try / catch

try:
    out = lf.collect_async(gevent=True)
except ImportError:
    out = lf.collect()  # or install gevent

Prevention

When it happens

Trigger: Calling LazyFrame.collect_async(gevent=True) or polars.collect_all_async(gevent=True) in an environment where gevent is not installed.

Common situations: Deploying async-collection code to production images that omit the gevent extra; running the gevent path on default asyncio-based setups; slim Docker images without optional dependencies.

Understand the failure class

Background: "X is not installed. Please install it with pip install Y": missing optional dependency errors — ImportError/ValueError raised when a library's optional extra was never installed — this error's family across 22 libraries.

Related errors


AI-assisted analysis of pola-rs/polars@fe841f959e (2026-09-18). Data as JSON: /api/errors/830263b3ed9dbc88. Report an issue: GitHub.

Appendix: source

Thrown at py-polars/src/polars/_utils/async_.py:29

    from collections.abc import Generator

    from polars import DataFrame
    from polars._plr import PyDataFrame


T = TypeVar("T")


class _GeventDataFrameResult(Generic[T]):
    __slots__ = ("_result", "_value", "_watcher")

    def __init__(self) -> None:
        if not _GEVENT_AVAILABLE:
            msg = (
                "gevent is required for using LazyFrame.collect_async(gevent=True) or"
                "polars.collect_all_async(gevent=True)"
            )
            raise ImportError(msg)

        from gevent.event import AsyncResult  # type: ignore[import-untyped]
        from gevent.hub import get_hub  # type: ignore[import-untyped]

        self._value: None | Exception | PyDataFrame | list[PyDataFrame] = None
        self._result = AsyncResult()

        self._watcher = get_hub().loop.async_()
        self._watcher.start(self._watcher_callback)

    def get(
        self,
        block: bool = True,  # noqa: FBT001
        timeout: float | int | None = None,
    ) -> T:
        return self.result.get(block=block, timeout=timeout)

    @property

View on GitHub (pinned to fe841f959e)