{"record":{"id":"d9e33be4f7dc52e2","repo":"pola-rs/polars","slug":"required-package-torch-not-installed-nplease-in","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/df599052daf96e7a9cc30a3b0c6bd25d6947e3c0/py-polars/src/polars/ml/torch.py#L11-L47","documentation":"The polars.ml.torch module (PolarsDataset and the dataset/dataloader return types of DataFrame.to_torch) imports torch at module import time. If torch is not installed, it raises ImportError with the exact pip command to fix it.","triggerScenarios":"from polars.ml.torch import PolarsDataset, or df.to_torch(return_type='dataset'/'dataloader'), in an environment where torch is absent.","commonSituations":"Slim production/CI images without ML extras; local venvs that installed polars only; deploying code developed where torch happened to be present.","solutions":["Install it: pip install torch (or uv add torch / poetry add torch)","Declare torch as an explicit project dependency wherever to_torch/PolarsDataset is used","If tensors are not needed, use df.to_numpy() instead; or gate the torch-dependent code path behind an availability check"],"exampleFix":"# before\nfrom polars.ml.torch import PolarsDataset  # ImportError in torch-less env\n\n# after\n# pip install torch\nfrom polars.ml.torch import PolarsDataset","handlingStrategy":"validation","validationCode":"import importlib.util\n\nif importlib.util.find_spec('torch') is None:\n    raise RuntimeError('install torch (pip install torch) before using to_torch datasets')","typeGuard":"import importlib.util\n\ndef torch_available() -> bool:\n    return importlib.util.find_spec('torch') is not None","tryCatchPattern":"try:\n    from polars.ml.torch import PolarsDataset\nexcept ImportError as exc:\n    raise RuntimeError('ML features require torch; run: pip install torch') from exc","preventionTips":["Treat torch as an optional extra; check availability before importing polars.ml modules","Pin torch in requirements for reproducible ML environments"],"tags":["polars","torch","import-error","optional-dependency"],"backgroundTag":null,"analyzedSha":"df599052daf96e7a9cc30a3b0c6bd25d6947e3c0","analyzedAt":"2026-08-16T12:10:03.978Z","schemaVersion":2},"datasetVersion":"2026-08-16T13:17:31.715Z"}