Colorful Wires

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

Create an executable file containing icecream with pyinstaller

When attempting to convert a Python script that utilizes the icecream library into an executable using pyinstaller, an error may occur. The error message states that a FileNotFoundError occurred, specifically for the file 'icecream/version.py'.

This error is caused by the icecream library running the icecream/version.py file with the exec() function, and therefore this file needs to be included in the executable.

A solution to this issue is to edit the .spec file for pyinstaller. First, add the following code at the top of the file:

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

Then, add the Tree() function as an argument to the COLLECT() function like so:

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

By implementing these changes, the icecream library will be properly included in the executable, and the error should be resolved.