# ITK Python pyinstaller issue

**URL:** https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520
**Category:** Uncategorized
**Created:** [January 11, 2019, 6:02pm UTC](https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520 "2019-01-11T18:02:12Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![sshamekhi](https://discourse.itk.org/user_avatar/discourse.itk.org/sshamekhi/32/578_2.png) [@sshamekhi](https://discourse.itk.org/u/sshamekhi)
#### Post date: [January 11, 2019, 6:02pm UTC](https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520/1 "2019-01-11T18:02:12Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![matt.mccormick](https://discourse.itk.org/user_avatar/discourse.itk.org/matt.mccormick/32/7_2.png) [@matt.mccormick](https://discourse.itk.org/u/matt.mccormick)
#### Post date: [January 13, 2019, 6:29pm UTC](https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520/2 "2019-01-13T18:29:30Z")

</div>

Hi @sshamekhi,

There is work in progress for this issue here:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/410>

> <https://github.com/InsightSoftwareConsortium/ITK/issues/214>

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

Thanks,  
Matt

---

<div class="post-metadata">

### Author: ![sshamekhi](https://discourse.itk.org/user_avatar/discourse.itk.org/sshamekhi/32/578_2.png) [@sshamekhi](https://discourse.itk.org/u/sshamekhi)
#### Post date: [January 17, 2019, 2:51pm UTC](https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520/3 "2019-01-17T14:51:04Z")

</div>

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

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [January 18, 2019, 7:17pm UTC](https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520/4 "2019-01-18T19:17:41Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![fbudin](https://discourse.itk.org/user_avatar/discourse.itk.org/fbudin/32/14_2.png) [@fbudin](https://discourse.itk.org/u/fbudin)
#### Post date: [January 21, 2019, 9:55pm UTC](https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520/5 "2019-01-21T21:55:55Z")

</div>

I created a [PR on the pyinstaller project](https://github.com/pyinstaller/pyinstaller/pull/3998) to facilitate using ITK with pyinstaller. I also update my previous answer to improve the hook file that can be used with pyinstaller.

---

<div class="post-metadata">

### Author: ![Ujhik](https://discourse.itk.org/user_avatar/discourse.itk.org/ujhik/32/703_2.png) [@Ujhik](https://discourse.itk.org/u/Ujhik)
#### Post date: [August 19, 2019, 10:15am UTC](https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520/6 "2019-08-19T10:15:29Z")

</div>

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

---

<div class="post-metadata">

### Author: ![guo7212](https://discourse.itk.org/letter_avatar_proxy/v4/letter/g/7ab992/32.png) [@guo7212](https://discourse.itk.org/u/guo7212)
#### Post date: [November 22, 2024, 7:25am UTC](https://discourse.itk.org/t/itk-python-pyinstaller-issue/1520/7 "2024-11-22T07:25:57Z")

</div>

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?
