microsoft / onnxruntime

ONNX Runtime: cross-platform, high performance ML inferencing and training accelerator
https://onnxruntime.ai
MIT License
13.93k stars 2.81k forks source link

[Build] ModuleNotFoundError: No module named 'onnxruntime.capi' #21317

Open lzivan opened 1 month ago

lzivan commented 1 month ago

Describe the issue

Hi team,

I'm using CPU build from source.

.\build.bat --config RelWithDebInfo --build_shared_lib --parallel --build_wheel --skip_tests

The build is successfully done. Now I'm using conda with python 3.11. The WHL file is under build\Windows\RelWithDebInfo\RelWithDebInfo\dist.

pip install onnxruntime-1.19.0-cp311-cp311-win_amd64.whl

However, when I'm running onnxruntime-python-api.py

It presents:

Traceback (most recent call last):
  File "C:\Users\arda\zijie\onnxruntime\onnxruntime-python-api.py", line 7, in <module>
    import onnxruntime
  File "C:\Users\arda\zijie\onnxruntime\onnxruntime\__init__.py", line 54, in <module>
    from onnxruntime.capi import onnxruntime_validation
ModuleNotFoundError: No module named 'onnxruntime.capi'

So I checked the site-packages of onnxruntime under conda envs, it does exist. image

Please help, I'm not sure whether some packages are lost during the installation from whl.

Urgency

No response

Target platform

Onnxruntime CPU

Build script

.\build.bat --config RelWithDebInfo --build_shared_lib --parallel --build_wheel --skip_tests

pip install onnxruntime-1.19.0-cp311-cp311-win_amd64.whl

Error / output

Traceback (most recent call last):
  File "C:\Users\arda\zijie\onnxruntime\onnxruntime-python-api.py", line 7, in <module>
    import onnxruntime
  File "C:\Users\arda\zijie\onnxruntime\onnxruntime\__init__.py", line 54, in <module>
    from onnxruntime.capi import onnxruntime_validation
ModuleNotFoundError: No module named 'onnxruntime.capi'

Visual Studio Version

No response

GCC / Compiler Version

No response

lzivan commented 1 month ago

This is my pip list after installation from WHL.

Package           Version
----------------- ---------
coloredlogs       15.0.1
filelock          3.15.4
flatbuffers       24.3.25
fsspec            2024.6.1
humanfriendly     10.0
intel-openmp      2021.4.0
Jinja2            3.1.4
MarkupSafe        2.1.5
mkl               2021.4.0
mpmath            1.3.0
networkx          3.3
numpy             2.0.0
onnxruntime       1.19.0
packaging         24.1
pip               24.0
protobuf          5.27.2
pyreadline3       3.4.1
setuptools        70.2.0
sympy             1.13.0
tbb               2021.13.0
torch             2.3.1
typing_extensions 4.12.2
wheel             0.43.0
mgehre-amd commented 1 month ago

I had the same problem when running python scripts from the onnxruntime git checkout, because it contains a folder named "onnxruntime" which gets confused with the onnxruntime package.

lzivan commented 1 month ago

Thanks for the response. I changed the name of the onnxruntime folder under the git repo, and it won't give me "onnxruntime.capi" error. But now it says "Module onnx is not installed".

According to the error log, "onnx" is called under "torch". For "torch", I manually installed it by "pip install" after installing the WHL file. So for "onnx", do I need to manually install it by "pip install onnx"? Btw "onnx" is not installed after installing the WHL.

Traceback (most recent call last):
  File "C:\Users\arda\miniforge3\envs\test-onnx\Lib\site-packages\torch\onnx\_internal\onnx_proto_utils.py", line 219, in _add_onnxscript_fn
    import onnx
ModuleNotFoundError: No module named 'onnx'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\Users\arda\zijie\onnxruntime\onnxruntime-python-api.py", line 127, in <module>
    main()
    ^^^^^^
  File "C:\Users\arda\zijie\onnxruntime\onnxruntime-python-api.py", line 109, in main
    create_model()
  File "C:\Users\arda\zijie\onnxruntime\onnxruntime-python-api.py", line 30, in create_model
    torch.onnx.export(model(), (sample_x, sample_y), MODEL_FILE, input_names=["x", "y"], output_names=["z"],
  File "C:\Users\arda\miniforge3\envs\test-onnx\Lib\site-packages\torch\onnx\utils.py", line 516, in export
    _export(
  File "C:\Users\arda\miniforge3\envs\test-onnx\Lib\site-packages\torch\onnx\utils.py", line 1686, in _export
    proto = onnx_proto_utils._add_onnxscript_fn(
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\arda\miniforge3\envs\test-onnx\Lib\site-packages\torch\onnx\_internal\onnx_proto_utils.py", line 221, in _add_onnxscript_fn
    raise errors.OnnxExporterError("Module onnx is not installed!") from e
torch.onnx.errors.OnnxExporterError: Module onnx is not installed!
github-actions[bot] commented 5 days ago

This issue has been automatically marked as stale due to inactivity and will be closed in 30 days if no further activity occurs. If further support is needed, please provide an update and/or more details.