ITK Python pyinstaller issue

Dear all
I am using ITKPython,
My codes are working well in PyCharm with *.py format but after making a standalone *.exe file via PyInstaller some errors occure as follow:

Traceback (most recent call last):
File “RSG_V_0.py”, line 27, in <module>
File “site-packages\itkExtras.py”, line 449, in imread
File “site-packages\itkLazy.py”, line 40, in getattribute
AttributeError: ‘LazyITKModule’ object has no attribute ‘ImageFileReader’
[29316] Failed to execute script RSG_V_0

Would you please show me the way to solve these kind of problems.
I attaches a simple .py,
By the way, already I test my PyInstaller with a simple print.py.

Hi @sshamekhi,

There is work in progress for this issue here:

You can subscribe to the issue on GitHub (using the Subscribe button on the right), to receive email updates on its progress.

Thanks,
Matt

2 Likes

Hi,
I will try it,
Thank you so much,

You can try to use a hook file with this content:

# This pyinstaller hook file does not support ITK with `WrapITK.pth`.

from PyInstaller.utils.hooks import collect_data_files

hiddenimports = ['new']

# If ITK is pip installed, gets all the files.
itk_datas = collect_data_files('itk', include_py_files=True)
datas = [x for x in itk_datas if '__pycache__' not in x[0]]

Save the code above in a file called hook-itk.py and add --additional-hooks-dir= to the command line calling pyinstaller, with the name of the folder in which you saved hook-itk.py.

If this works for you, I will ask pyinstaller if they can ship this hook file directly in their project.
Thanks!

2 Likes

I created a PR on the pyinstaller project to facilitate using ITK with pyinstaller. I also update my previous answer to improve the hook file that can be used with pyinstaller.

2 Likes

Thanks for the info.

The hook works for me with itk 5.0.1 wrapped in Ubuntu 18 64 bits. I built itk and wrapped it with .pth file.

Saved the hook content in a file “hook-itk.py” in a dir represented by DIR_PATH. Then called:

pyinstaller --additional-hooks-dir=DIR_PATH helloWorld.py

helloWorld.py contains:

import itk

ImageType = itk.Image[itk.UL, 3]
image = ImageType.New()

print(“ITK Hello World!”)
print(image)

print(itk.Version.GetITKVersion())

The generated executable works as intended, its output:

ITK Hello World!
Image (0x5619fe239710)
RTTI typeinfo: itk::Image<unsigned long, 3u>
Reference Count: 1
Modified Time: 1
Debug: Off
Object Name:
Observers:
none
Source: (none)
Source output name: (none)
Release Data: Off
Data Released: False
Global Release Data: Off
PipelineMTime: 0
UpdateMTime: 0
RealTimeStamp: 0 seconds
LargestPossibleRegion:
Dimension: 3
Index: [0, 0, 0]
Size: [0, 0, 0]
BufferedRegion:
Dimension: 3
Index: [0, 0, 0]
Size: [0, 0, 0]
RequestedRegion:
Dimension: 3
Index: [0, 0, 0]
Size: [0, 0, 0]
Spacing: [1, 1, 1]
Origin: [0, 0, 0]
Direction:
1 0 0
0 1 0
0 0 1

IndexToPointMatrix:
1 0 0
0 1 0
0 0 1

PointToIndexMatrix:
1 0 0
0 1 0
0 0 1

Inverse Direction:
1 0 0
0 1 0
0 0 1

PixelContainer:
ImportImageContainer (0x5619fd740260)
RTTI typeinfo: itk::ImportImageContainer<unsigned long, unsigned long>
Reference Count: 1
Modified Time: 2
Debug: Off
Object Name:
Observers:
none
Pointer: 0
Container manages memory: true
Size: 0
Capacity: 0

5.0.1

1 Like

Hi, all. I have installed pyinstaller (version: 6.11.1) and pyinstaller-hooks-contrib(version: 2024.10) with python-3.10 on Windows 10. And now there is no need to use –additional-hooks-dir. The exe is built successfully. But when I run the exe, there is an error as follows:

FileNotFoundError: [Errno 2] No such file or directory: ‘C:\Users\guo72\AppData\Local\Temp\_MEI302202\itk\support\…\ITKPyBasePython.py’
[PYI-36460:ERROR] Failed to execute script ‘TestITK’ due to unhandled exception!

Can someone help?