pythonpython-3.xpyinstallercasadi

adding hidden imports in .spec file doesnt seem to change anything pyinstaller


I have been playing around with compiling an app with pyinstaller but it doesn't seem to work properly.

my .spec file contains the following line in the Analysis object decleration: hiddenimports=['casadi', 'casadi._casadi'],

yet i still get this issue when i run the app (this is the full error):

Traceback (most recent call last):
File "casadi\casadi.py", line 18, in swig_import_helper
File "importlib\__init__.py", line 126, in import_module
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1004, in _find_and_load_unlocked ModuleNotFoundError: No module named 'casadi._casadi'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "main_screen.py", line 8, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 457, in exec_module File "cadquery\__init__.py", line 40, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 457, in exec_module File "cadquery\assembly.py", line 22, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 457, in exec_module File "cadquery\occ_impl\solver.py", line 17, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 457, in exec_module
File "casadi\__init__.py", line 36, in <module>
File "PyInstaller\loader\pyimod02_importers.py", line 457, in exec_module
File "casadi\casadi.py", line 21, in <module>
File "casadi\casadi.py", line 20, in swig_import_helper
File "importlib\__init__.py", line 126, in import_module
ImportError: DLL load failed while importing _casadi: The specified module could not be found. [PYI-21168:ERROR] Failed to execute script 'main_screen' due to unhandled exception!

after a little digging, this happens when I import the "exporters" module from "cadquery".

this error can also be reproduced by creating a file:

/foo.py

that contains only the following line:

from cadquery import exporters

and running:

pyinstaller --onefile --hidden-import casadi --hidden-import casadi._casadi foo.py

I'm confused because I know the module is imported with 'importlib.load_module()' and pyinstaller doesnt analyze this import, so you need to manually bundle this in the app. yet i seem to have bundeled it and it doesnt work. why is that?

P.s:, I didnt get any compliation errors in the console when i ran pyinstaller on the .spec file

for extra reference, this is my repo: https://github.com/YahliGilboa/tray_creator/tree/master

thanks in advance!


Solution

  • FIGURED IT OUT. indeed, it was the matter of adding the right binary to the correct package manually (it didnt work when i just tried to add it straight to '.', i had to manually add it to casadi package).

    this was done by adding the following line to .spec file under Analysis config:
    binaries=[('venv/Lib/site-packages/casadi/_casadi.pyd','casadi')]

    (.pyd is like a DLL designed for python)
    thanks to @furas for pointing this out!