Title: What is a .pyci file when compiling executables using pyinstaller?
Tags: python,pyinstaller,scikit-image,easyocr

Question: I am trying to package a python application using PyInstaller. I used the following command:
pyinstaller --noconfirm --onedir --windowed --icon "D:/Development/nikke-assistant/images/nikke_icon.ico" --add-data "C:/Program Files/Tesseract-OCR;Tesseract-OCR/" --hidden-import "skimage" --paths "C:/Windows/System32/downlevel" --hidden-import "easyocr" --collect-all "easyocr" --collect-all "scikit-image" --runtime-hook "D:/Development/nikke-assistant/hook.py"  "D:/Development/nikke-assistant/nikke_interface.py"

When running the executable, I run into the following error:
Traceback (most recent call last):
  File "nikke_interface.py", line 11, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "nikke_agent.py", line 9, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "game_interaction_io.py", line 7, in <module>
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "easyocr\__init__.py", line 1, in <module>
    from .easyocr import Reader
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "easyocr\easyocr.py", line 3, in <module>
    from .recognition import get_recognizer, get_text
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "easyocr\recognition.py", line 10, in <module>
    from .utils import CTCLabelConverter
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "easyocr\utils.py", line 13, in <module>
    from .imgproc import loadImage
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "easyocr\imgproc.py", line 8, in <module>
    from skimage import io
  File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
  File "PyInstaller\loader\pyimod02_importers.py", line 352, in exec_module
  File "skimage\__init__.py", line 74, in <module>
  File "lazy_loader\__init__.py", line 243, in attach_stub
ValueError: Cannot load imports from non-existent stub 'D:\\Development\\nikke-assistant\\output\\nikke_interface\\skimage\\__init__.pyci'


I've traced down the issue and apparently it caused by a chain of imports:

imports easyocr
easyocr imports skimage
skimage has a module called io
To import modules in the skimage package, it uses a library called lazy_loader to lazy load the dependencies where it does something like below in the __init__.py file:

import lazy_loader as lazy
__getattr__, __lazy_dir__, _ = lazy.attach_stub(__name__, __file__)

The library lazy_loader looks at another stub file with the name of __init__.pyi when loading the submodules.
The key question is: what is the skimage\\__init__.pyci it's looking for? I know .pyc or .pyi,  is it looking for a compiled version of the stub file?

Answer: I had the same issue too, and i solved it this way: put the init.pyi in in the skimage dir in the dist BUT rename it init.pyci, copy skimage/data dir into the skimage dist dir, and rename again the init.pyi to pyci. IT WORKS :) , ma config is python 3.11, pyInstaller 3.9