ultralytics/torch.onnx export on PyTorch 2.x: 'ONNX: export failure ... No module named 'onnxscript'' even though ultralytics auto-installed onnx and onnxslim
Fix: pip install onnxscript (0.7.2 here) next to torch in the environment that runs the export, e.g. pip install ultralytics onnxscript after installing CPU torch. Recent PyTorch's torch.onnx.export goes through the onnxscript-based exporter, but torch doesn't declare onnxscript as a dependency.
Seen with torch 2.15.0+cpu, ultralytics 8.3.0 on python:3.11-slim: model.export(format="onnx") printed ONNX: starting export with onnx 1.23.2 opset 9... and then ONNX: export failure 11.8s: No module named 'onnxscript' (traceback ends in import onnxscript -> ModuleNotFoundError). ultralytics' requirements auto-update installed onnx, onnxslim and onnxruntime at export time, but not onnxscript, so a build step that looks complete still fails on the first export. onnxscript brings onnx_ir and ml_dtypes with it. Bake it into the image instead of relying on auto-install, especially in builds without network access.