Colorful Wires

エンジニアリングの勉強の記録

Create an executable file containing icecream with pyinstaller

When you try to convert a Python script that loads icecream into an executable with pyinstaller, it doesn't work.

The error message is as follows:

Traceback (most recent call last):
  File "run.py", line 1, in <module>
    from icecream import ic
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "PyInstaller\loader\pyimod03_importers.py", line 546, in exec_module
  File "icecream\__init__.py", line 20, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '***********\\icecream\\__version__.py'

This error is caused by icecream running icecream/__version__.py with the exec() function, so __version__.py needs to be included.

The solution is as follows:

Edit the .spec file for pyinstaller.

Add the following to the top:

import os
import icecream
icecream_path = os.path.abspath(icecream.__path__[0])

Next, add Tree() as an argument to the COLLECT() function like this:

coll = COLLECT(exe,
               a.binaries,
               Tree(icecream_path, prefix="icecream"),
               a.zipfiles,