pyinstaller itk failed to execute script due to unhandled exception

Hi all,

I’m trying to write an executable of my python program using PyInstaller. I tried following the below article (using the same test program in said article)

by adding a hook and then writing

python -m PyInstaller --additional-hooks-dir C:\Users\path\hook-itk.py itk-test.py

but it still gives me an error when i try and open the exe file in the dist folder:

WARNING: Internal configuration path is invalid: C:\Users\path\dist\itk-test\_internal\itk
WARNING: Invalid: swig_lib: location of the swig-generated shared libraries
WARNING: Internal configuration path is invalid: C:\Users\path\dist\itk-test\_internal\itk
WARNING: Invalid: swig_py: location of the xxxPython.py swig-generated python interfaces
WARNING: Internal configuration path is invalid: C:\Users\path\itk-test\_internal\itk\Configuration
WARNING: Invalid: config_py: location of xxxConfig.py CMake-generated library descriptions
Traceback (most recent call last):
  File "itk-test.py", line 8, in <module>
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "itk\__init__.py", line 179, in <module>
  File "itk\__init__.py", line 73, in _initialize_module
  File "<frozen importlib._bootstrap>", line 1178, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1149, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 391, in exec_module
  File "itk\support\base.py", line 473, in <module>
  File "itk\support\base.py", line 460, in _initialize
RuntimeError: WARNING: Invalid configuration directory requested: C:\Users\path\dist\itk-test\_internal\itk\Configuration
[7808] Failed to execute script 'itk-test' due to unhandled exception!

If it helps, I don’t see an itk folder C:\Users\path\dist\itk-test_internal\itk at all.


^So I tried it again, this time with
python -m PyInstaller --additional-hooks-dir=C:\Users\path itk-test.py

And I got the folder to appear but I still can’t get it to open:

Traceback (most recent call last):
File “itk-test.py”, line 10, in
File “itk\support\lazy.py”, line 138, in getattribute
File “itk\support\base.py”, line 96, in itk_load_swig_module
File “itk\support\base.py”, line 132, in itk_load_swig_module
File “itk\support\base.py”, line 291, in load
File “”, line 940, in exec_module
File “”, line 241, in _call_with_frames_removed
File “C:\Users\path\dist\itk-test_internal\itk\support..\ITKPyBasePython.py”, line 13, in
from . import _ITKPyBasePython
ImportError: cannot import name 'ITKPyBasePython’ from ‘itk’ (C:\Users\path\dist\itk-test_internal\itk_init.pyc)
[22072] Failed to execute script ‘itk-test’ due to unhandled exception!

Ok so I found a way to make it work but I don’t know if I broke anything while doing it.

Basically, go to C:\Users\path\dist\itk-test_internal

Then replace all the itk related folders here with the ones in the site-packages folder of the relevant Python install C:\Users.…\Python\Python311\site-packages

And it works! I could always delete the dist folder in case something bad happens so I figured why not play around with it. I’m still not sure why the ones created using PyInstaller don’t have an underscore whereas those in the site packages have one (e.g. ITKPyBasePython vs _ITKPyBasePython) but if it works it works.

1 Like