onnx / keras-onnx

Convert tf.keras/Keras models to ONNX
Apache License 2.0
379 stars 110 forks source link

Pyinstaller cannot handle dependencies of this library #729

Closed bulatnv closed 3 years ago

bulatnv commented 3 years ago

Could not to build even a simple script with this library. Similar issue

Environment: 1) Windows 10 2) Python 3.6.5 3) PyCharm, venv. 4) Main libraries: onnx~=1.7.0, tensorflow~=2.3.1, keras2onnx~=1.7.0, pyinstaller~=4.0

Steps to reproduce: 1) Create environment 2) Install keras2onnx==1.7.0, pyinstaller>=4.0 3) Create simple script "test.py"

import keras2onnx
print('Hello world!')

4) Build script pyinstaller test.py --onefile

5) Run build test.exe

6) Output:

2021-07-01 13:25:22.750354: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-07-01 13:25:22.750520: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "test.py", line 1, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "d:\projects\other projects\changedetectiononnxexporter\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "keras2onnx\__init__.py", line 30, in <module>
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "d:\projects\other projects\changedetectiononnxexporter\venv\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 493, in exec_module
    exec(bytecode, module.__dict__)
  File "keras2onnx\proto\__init__.py", line 25, in <module>
  File "keras2onnx\proto\__init__.py", line 21, in _check_onnx_version
  File "pkg_resources\__init__.py", line 481, in get_distribution
  File "pkg_resources\__init__.py", line 357, in get_provider
  File "pkg_resources\__init__.py", line 900, in require
  File "pkg_resources\__init__.py", line 786, in resolve
pkg_resources.DistributionNotFound: The 'onnx' distribution was not found and is required by the application
[10208] Failed to execute script test

Do not look at Tensorflow warnings, they definitely work.

Is there any way of building this library with pyinstaller. Any help is appreciated.

bulatnv commented 3 years ago

Giving answer to my own question:

Somehow pyinstaller can't handle method _check_onnx_version() in proto.__init__

I think it is because of shadow import of pkg_resources

Just bypassing the problem by commenting out _check_onnx_version() method.