pola-rs/polars · error · ImportError

Required package 'torch' not installed. Please install it us

Error message

Required package 'torch' not installed.
Please install it using the command `pip install torch`.

What it means

Import-time failure in polars.ml.torch: the `torch` package (imported together with torch.utils.data.TensorDataset) is not installed, so the PolarsDataset helper built on TensorDataset cannot be defined and the module cannot load at all.

Source

Thrown at py-polars/src/polars/ml/torch.py:29

if TYPE_CHECKING:
    import sys
    from collections.abc import Sequence

    from torch import Tensor, memory_format

    if sys.version_info >= (3, 11):
        from typing import Self
    else:
        from typing_extensions import Self
try:
    import torch
    from torch.utils.data import TensorDataset
except ImportError:
    msg = (
        "Required package 'torch' not installed.\n"
        "Please install it using the command `pip install torch`."
    )
    raise ImportError(msg) from None


__all__ = ["PolarsDataset"]


class PolarsDataset(TensorDataset):  # type: ignore[misc]
    """
    TensorDataset class specialized for use with Polars DataFrames.

    .. warning::
        This functionality is considered **unstable**. It may be changed
        at any point without it being considered a breaking change.

    Parameters
    ----------
    frame
        Polars DataFrame containing the data that will be retrieved as Tensors.
    label

View on GitHub (pinned to 9b5d73fd00)

Solutions

  1. Install torch: pip install torch.
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at py-polars/src/polars/ml/torch.py:29 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of pola-rs/polars@9b5d73fd00 (2026-08-19). Data as JSON: /api/errors/3a4d58f04fd0617f. Report an issue: GitHub.