Commit c1240a2
authored
### Description
Fixes #8455. Installing a broken/hanging onnx (e.g. `onnx==1.18.0` on
Windows) makes `import monai` fail with no error message.
Root cause: `monai/networks/utils.py` and `monai/bundle/scripts.py` call
`optional_import("onnx")` (and `onnx.reference` / `onnxruntime`) at
module scope. `optional_import` imports eagerly (`__import__`), and
`import monai` auto-loads `monai.networks`, so importing MONAI
unconditionally imports onnx — inheriting any onnx import failure/hang.
This defers those optional imports into the functions that actually use
them (`convert_to_onnx` in `networks/utils.py`, `onnx_export`'s
`save_onnx` in `bundle/scripts.py`), matching the lazy pattern already
used for `tensorrt` in the same file. After this change, importing MONAI
no longer imports onnx; the ONNX conversion code paths are unchanged.
### Types of changes
- [x] Non-breaking change (fix or new feature that would not break
existing functionality).
- [x] New tests added to cover the changes.
### Testing
Added `tests/networks/test_lazy_onnx_import.py`, which (in a subprocess,
with a meta-path recorder) asserts that `import monai` and `import
monai.bundle` do not import onnx/onnxruntime — verified passing with
onnx installed. The existing `tests/networks/test_convert_to_onnx.py`
continues to exercise the conversion paths in CI.
Signed-off-by: Lanre Shittu <136805224+Shizoqua@users.noreply.github.com>
1 parent e04a802 commit c1240a2
3 files changed
Lines changed: 56 additions & 4 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
59 | 59 | | |
60 | 60 | | |
61 | 61 | | |
62 | | - | |
63 | 62 | | |
64 | 63 | | |
65 | 64 | | |
| |||
1437 | 1436 | | |
1438 | 1437 | | |
1439 | 1438 | | |
| 1439 | + | |
1440 | 1440 | | |
1441 | 1441 | | |
1442 | 1442 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | 37 | | |
41 | 38 | | |
42 | 39 | | |
| |||
709 | 706 | | |
710 | 707 | | |
711 | 708 | | |
| 709 | + | |
| 710 | + | |
712 | 711 | | |
713 | 712 | | |
714 | 713 | | |
| |||
778 | 777 | | |
779 | 778 | | |
780 | 779 | | |
| 780 | + | |
781 | 781 | | |
782 | 782 | | |
783 | 783 | | |
784 | 784 | | |
785 | 785 | | |
| 786 | + | |
786 | 787 | | |
787 | 788 | | |
788 | 789 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
0 commit comments