Packaging modules which use lazy_loader

51 views
Skip to first unread message

Clemens Brunner

unread,
Feb 17, 2025, 5:44:49 AMFeb 17
to PyInstaller
Hello!

I'm trying to package a Python script which imports mne, a package that uses lazy loading (via the lazy_loader package).

To reproduce, create a script called mwe.py with the following content:

import mne
print(mne.sys_info())

Then run pyinstaller mwe.py, and try executing the generated app with dist/mwe/mwe. This gives the following error (I tried this on macOS):

Traceback (most recent call last):
  File "mwe.py", line 1, in <module>
    import mne
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "PyInstaller/loader/pyimod02_importers.py", line 450, in exec_module
  File "mne/__init__.py", line 30, in <module>
  File "lazy_loader/__init__.py", line 342, in attach_stub
ValueError: Cannot load imports from non-existent stub '/Users/clemens/Desktop/test-pyinstaller-pyi/dist/mwe/_internal/mne/__init__.pyi'
[PYI-16568:ERROR] Failed to execute script 'mwe' due to unhandled exception!


Any ideas/suggestions on how to make this work? I saw that skimage had a similar problem, but I don't know how to make it work for mne. Also, it seems like some packages have adopted lazy loading, such as numpy and scipy, and it would probably be nice to have a solution that works for all of these packages.

Thanks!

Clemens

Clemens Brunner

unread,
Feb 19, 2025, 4:00:27 AMFeb 19
to PyInstaller
It looks like

pyinstaller --collect-all mne mwe.py

is all it takes to build the example.
Reply all
Reply to author
Forward
0 new messages