{"record":{"id":"4bc00efb0011eff6","repo":"Lightning-AI/pytorch-lightning","slug":"type-self-name-to-onnx-dynamo-true-requi","errorCode":null,"errorMessage":"`{type(self).__name__}.to_onnx(dynamo=True)` requires `onnxscript` to be installed.","messagePattern":"`(.+?)\\.to_onnx\\(dynamo=True\\)` requires `onnxscript` to be installed\\.","errorType":"exception","errorClass":"ModuleNotFoundError","httpStatus":null,"severity":"error","filePath":"src/lightning/pytorch/core/module.py","lineNumber":1485,"sourceCode":"\n            class SimpleModel(LightningModule):\n                def __init__(self):\n                    super().__init__()\n                    self.l1 = torch.nn.Linear(in_features=64, out_features=4)\n\n                def forward(self, x):\n                    return torch.relu(self.l1(x.view(x.size(0), -1)\n\n            model = SimpleModel()\n            input_sample = torch.randn(1, 64)\n            model.to_onnx(\"export.onnx\", input_sample, export_params=True)\n\n        \"\"\"\n        if not _ONNX_AVAILABLE:\n            raise ModuleNotFoundError(f\"`{type(self).__name__}.to_onnx()` requires `onnx` to be installed.\")\n\n        if kwargs.get(\"dynamo\", False) and not _ONNXSCRIPT_AVAILABLE:\n            raise ModuleNotFoundError(\n                f\"`{type(self).__name__}.to_onnx(dynamo=True)` requires `onnxscript` to be installed.\"\n            )\n\n        mode = self.training\n\n        if input_sample is None:\n            if self.example_input_array is None:\n                raise ValueError(\n                    \"Could not export to ONNX since neither `input_sample` nor\"\n                    \" `model.example_input_array` attribute is set.\"\n                )\n            input_sample = self.example_input_array\n\n        input_sample = self._on_before_batch_transfer(input_sample)\n        input_sample = self._apply_batch_transfer_handler(input_sample)\n\n        file_path = str(file_path) if isinstance(file_path, Path) else file_path\n        # PyTorch (2.5) declares file_path to be str | PathLike[Any] | None, but","sourceCodeStart":1467,"sourceCodeEnd":1503,"githubUrl":"https://github.com/Lightning-AI/pytorch-lightning/blob/9fed5c27d2a62ff0efd6c3573599921d6ff67c14/src/lightning/pytorch/core/module.py#L1467-L1503","documentation":"to_onnx(dynamo=True) uses the new torch.onnx.dynamo_export path, which is implemented via `onnxscript`. Lightning checks `_ONNXSCRIPT_AVAILABLE` and raises ModuleNotFoundError when dynamo export is requested but onnxscript is missing.","triggerScenarios":"Calling `model.to_onnx(path, sample, dynamo=True)` (or passing dynamo=True in kwargs) without `onnxscript` installed.","commonSituations":"Switching to the PyTorch 2.x dynamo-based ONNX exporter on an environment built for the legacy torch.onnx.export path.","solutions":["pip install onnxscript","Or drop dynamo=True to use the legacy exporter (only needs `onnx`)","Confirm version compatibility between torch, onnx, and onnxscript"],"exampleFix":"# before\nmodel.to_onnx(\"m.onnx\", x, dynamo=True)  # ModuleNotFoundError\n# after\n# pip install onnxscript\nmodel.to_onnx(\"m.onnx\", x, dynamo=True)","handlingStrategy":"validation","validationCode":"import importlib.util\nuse_dynamo = importlib.util.find_spec(\"onnxscript\") is not None\nmodel.to_onnx(path, sample, dynamo=use_dynamo)","typeGuard":null,"tryCatchPattern":"try:\n    model.to_onnx(path, sample, dynamo=True)\nexcept ModuleNotFoundError as e:\n    if \"onnxscript\" in str(e):\n        model.to_onnx(path, sample)  # legacy path\n    else:\n        raise","preventionTips":["Pin onnxscript alongside torch in requirements","Gate dynamo usage on find_spec('onnxscript')"],"tags":["onnx","dynamo","onnxscript","missing-dependency","lightning"],"backgroundTag":"missing-optional-dependency","analyzedSha":"9fed5c27d2a62ff0efd6c3573599921d6ff67c14","analyzedAt":"2026-08-28T11:52:41.083Z","schemaVersion":2},"datasetVersion":"2026-08-28T16:17:29.566Z"}