Skip to content

onnx==1.18.0 is causing MONAI to fail to import on Windows #8455

Description

@joshuacwnewton

Describe the bug

When installing onnx==1.18.0 alongside monai==1.4.0 on Windows, the import fails with no exception or log message.

To Reproduce

Steps to reproduce the behavior:

  1. Create a fresh Python 3.9/3.10 conda environment (may affect other versions as well)

  2. Install the following list of packages using pip install -r requirements.txt (note: we first install onnx==1.16.1 to showcase a working install):

    Details
    absl-py==2.2.2
    acres==0.3.0
    acvl_utils==0.2
    annotated-types==0.7.0
    astor==0.8.1
    batchgenerators==0.25.1
    bids-validator==1.14.7.post0
    bidsschematools==1.0.7
    certifi==2025.4.26
    charset-normalizer==3.4.2
    click==8.1.8
    colorama==0.4.6
    coloredlogs==15.0.1
    connected-components-3d==3.23.0
    contourpy==1.3.0
    coverage==7.8.0
    csv-diff==1.2
    cycler==0.12.1
    Cython==3.1.0
    deepdiff==8.5.0
    Deprecated==1.2.18
    dicom2nifti==2.6.0
    dictdiffer==0.9.0
    dipy==1.8.0
    docker-pycreds==0.4.0
    docopt==0.6.2
    dynamic_network_architectures==0.3.1
    einops==0.8.1
    eval_type_backport==0.2.2
    exceptiongroup==1.3.0
    filelock==3.18.0
    flatbuffers==25.2.10
    fonttools==4.58.0
    formulaic==0.3.4
    fsspec==2025.3.2
    future==1.0.0
    gitdb==4.0.12
    GitPython==3.1.44
    graphviz==0.20.3
    grpcio==1.71.0
    gryds==0.0.9
    h5py==3.13.0
    humanfriendly==10.0
    humanize==4.12.3
    idna==3.10
    imagecodecs==2024.12.30
    imageio==2.37.0
    importlib_metadata==8.7.0
    importlib_resources==6.5.2
    iniconfig==2.1.0
    interface-meta==1.3.0
    ivadomed==2.9.10
    Jinja2==3.1.6
    joblib==1.5.0
    kiwisolver==1.4.7
    lazy_loader==0.4
    linecache2==1.0.0
    loguru==0.7.3
    lxml==5.4.0
    Markdown==3.8
    markdown-it-py==3.0.0
    MarkupSafe==3.0.2
    matplotlib==3.9.4
    matplotlib-inline==0.1.7
    mdurl==0.1.2
    monai==1.4.0
    mpmath==1.3.0
    msvc-runtime==14.29.30133
    networkx==3.2.1
    neurite==0.2
    nibabel==5.3.2
    nilearn==0.10.4
    nnunetv2==2.3.1
    num2words==0.5.14
    numpy==1.26.4
    onnx==1.16.1
    onnxruntime==1.19.2
    orderly-set==5.4.1
    packaging==25.0
    pandas==1.5.3
    pillow==11.2.1
    platformdirs==4.3.8
    pluggy==1.6.0
    portalocker==3.1.1
    protobuf==6.31.0
    psutil==7.0.0
    pybids==0.15.5
    pydantic==2.11.4
    pydantic_core==2.33.2
    pydicom==2.4.4
    Pygments==2.19.1
    pyparsing==3.2.3
    PyQt5==5.12.3
    PyQt5-sip==12.12.2
    pyreadline3==3.5.4
    pystrum==0.4
    pytest==8.3.5
    pytest-console-scripts==1.4.1
    pytest-cov==6.1.1
    python-dateutil==2.9.0.post0
    python-gdcm==3.0.24.1
    pytz==2025.2
    pywin32==310
    PyYAML==6.0.2
    requests==2.32.3
    requirements-parser==0.11.0
    rich==14.0.0
    scikit-image==0.24.0
    scikit-learn==1.6.1
    scipy==1.13.1
    seaborn==0.13.2
    sentry-sdk==2.28.0
    setproctitle==1.3.6
    setuptools-scm==8.3.1
    shellingham==1.5.4
    simpleitk==2.5.0
    six==1.17.0
    smmap==5.0.2
    # Editable install with no version control (spinalcordtoolbox==7.1.dev0)
    -e c:\users\joshua\sct_7.1.dev0
    SQLAlchemy==1.3.24
    sympy==1.14.0
    tensorboard==2.19.0
    tensorboard-data-server==0.7.2
    threadpoolctl==3.6.0
    tifffile==2024.8.30
    tomli==2.2.1
    torch==2.2.2+cpu
    torchio==0.20.8
    torchvision==0.17.2+cpu
    totalspineseg==20250205
    tqdm==4.67.1
    traceback2==1.4.0
    traitlets==5.14.3
    transforms3d==0.4.2
    trx-python==0.3
    typer==0.15.4
    types-setuptools==80.7.0.20250516
    typing-inspection==0.4.0
    typing_extensions==4.13.2
    unittest2==1.1.0
    urllib3==2.4.0
    voxelmorph==0.2
    wandb==0.19.11
    Werkzeug==3.1.3
    win32_setctime==1.2.0
    wquantiles==0.6
    wrapt==1.17.2
    XlsxWriter==3.2.3
    xlwt==1.3.0
    yacs==0.1.8
    zipp==3.21.0
    
  3. Run the command python -c "import monai; print('it worked!')", and the message will be printed.

  4. Now, install onnx==1.18.0 into the environment instead.

  5. Re-running the command python -c "import monai; print('it worked!')" will not print any message, or even an exception.

Additional context

onnx==1.18.0 and monai==1.4.0 are not enough to fully reproduce the issue. It seems some other package among my environment is contributing to this issue.

To better understand this issue, I tried comparing the import monai commands using the python -v option:

  • onnx==1.18.0 + monai==1.4.0 + extra packages: Failure (output.txt)
  • onnx==1.16.1 + monai==1.4.0 + extra packages: Success (output.txt)

Diffing these outputs, we can see that the outputs begin to diverge when importing onnx, specifically:

import 'onnx.serialization' # <_frozen_importlib_external.SourceFileLoader object at <0x00000200DD729520>
# extension module 'onnx.onnx_cpp2py_export' loaded from 'C:\\Users\\Joshua\\sct_7.1.dev0\\python\\envs\\venv_sct\\lib\\site-packages\\onnx\\onnx_cpp2py_export.cp39-win_amd64.pyd'

Also, the failing import halts specifically at these lines:

import 'onnx.model_container' # <_frozen_importlib_external.SourceFileLoader object at <0x00000200E84DF2E0>
# C:\Users\Joshua\sct_7.1.dev0\python\envs\venv_sct\lib\site-packages\onnx\reference\__pycache__\op_run.cpython-39.pyc matches C:\Users\Joshua\sct_7.1.dev0\python\envs\venv_sct\lib\site-packages\onnx\reference\op_run.py
# code object from 'C:\\Users\\Joshua\\sct_7.1.dev0\\python\\envs\\venv_sct\\lib\\site-packages\\onnx\\reference\\__pycache__\\op_run.cpython-39.pyc'

I am not sure why my combination of packages causes this difference, and I am also not sure why there is no failure for just onnx or just onnx+monai.

I am also not sure whether to report this issue on the monai repo or the onnx repo. But, since this issue doesn't seem to be affecting import onnx alone, I thought to report it here as well.

Given the mention of onnx_cpp2py_export specifically, I think it might be related to:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions