{"record":{"id":"3a4d58f04fd0617f","repo":"pola-rs/polars","slug":"required-package-torch-not-installed-please-ins","errorCode":null,"errorMessage":"Required package 'torch' not installed.\nPlease install it using the command `pip install torch`.","messagePattern":"Required package 'torch' not installed\\.\nPlease install it using the command `pip install torch`\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"py-polars/src/polars/ml/torch.py","lineNumber":29,"sourceCode":"if TYPE_CHECKING:\n    import sys\n    from collections.abc import Sequence\n\n    from torch import Tensor, memory_format\n\n    if sys.version_info >= (3, 11):\n        from typing import Self\n    else:\n        from typing_extensions import Self\ntry:\n    import torch\n    from torch.utils.data import TensorDataset\nexcept ImportError:\n    msg = (\n        \"Required package 'torch' not installed.\\n\"\n        \"Please install it using the command `pip install torch`.\"\n    )\n    raise ImportError(msg) from None\n\n\n__all__ = [\"PolarsDataset\"]\n\n\nclass PolarsDataset(TensorDataset):  # type: ignore[misc]\n    \"\"\"\n    TensorDataset class specialized for use with Polars DataFrames.\n\n    .. warning::\n        This functionality is considered **unstable**. It may be changed\n        at any point without it being considered a breaking change.\n\n    Parameters\n    ----------\n    frame\n        Polars DataFrame containing the data that will be retrieved as Tensors.\n    label","sourceCodeStart":11,"sourceCodeEnd":47,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/py-polars/src/polars/ml/torch.py#L11-L47","documentation":"The polars.ml.torch module requires the optional third-party package torch. Importing the module (e.g. pl.ml.torch or PolarsDataset) without torch installed raises this ImportError at module load time.","triggerScenarios":"Importing py-polars/src/polars/ml/torch.py (or PolarsDataset) in an environment where torch is not installed.","commonSituations":"Deploying to a slim environment without ML extras; using to_torch/PolarsDataset features without installing the optional dependency.","solutions":["Run `pip install torch`","Or install polars with the torch extra if available (e.g. pip install \"polars[torch]\")","If torch isn't needed, avoid importing the polars.ml.torch module"],"exampleFix":"// before\nfrom polars.ml.torch import PolarsDataset\n// after\n# shell: pip install torch\nfrom polars.ml.torch import PolarsDataset","handlingStrategy":"fallback","validationCode":"import importlib.util\nif importlib.util.find_spec(\"torch\") is None:\n    raise RuntimeError(\"install torch: pip install torch\")","typeGuard":null,"tryCatchPattern":"try:\n    from polars.ml.torch import PolarsDataset\nexcept ImportError:\n    PolarsDataset = None  # degrade gracefully or prompt install","preventionTips":["Install ML extras in CI and prod: pip install torch","Guard optional imports with importlib.util.find_spec","Split torch-dependent code paths behind lazy imports"],"tags":["python","polars","optional-dependency","torch"],"backgroundTag":"missing-optional-dependency","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}