Pyinstaller and Transformer Pipeline do not work well

334 views
Skip to first unread message

Han

unread,
Jun 2, 2025, 5:31:45 PMJun 2
to PyInstaller
Hi guys,

May I ask some questions regarding using Pyinstaller to package a Python project using Transformers. Seems that Pyinstaller does not work with dynamic lazy import well, so I explicitly add them (transformers) to the hidden import. But it still did not work as expected. The following is the details.
My code has this
from transformers import pipeline

The following is my .spec file:
from PyInstaller.utils.hooks import collect_all, collect_data_files

# Collect all necessary files from the 'transformers' package
transformers_datas = collect_data_files('transformers', include_py_files=True)
transformers_binaries, transformers_hiddenimports = collect_all('transformers')[1:]

a = Analysis(
['main.py'],
pathex=['.', './.venv/lib/python3.12/site-packages'],
binaries=transformers_binaries,
datas=transformers_datas,
hiddenimports=transformers_hiddenimports,
hookspath=[],
runtime_hooks=[],
excludes=[],
noarchive=False,
)

pyz = PYZ(a.pure)

exe = EXE(
pyz,
a.scripts,
[],
exclude_binaries=True,
name='some_app',
debug=False,
bootloader_ignore_signals=False,
strip=False,
upx=True,
console=False,
icon='resources/icon.icns',
)

coll = COLLECT(
exe,
a.binaries,
a.datas,
strip=False,
upx=True,
name='some_app',
)

app = BUNDLE(
coll,
name='some_app.app',
icon='resources/icon.icns',
bundle_identifier=None,
)

After building the package and running the executable, I got the following error.
Traceback (most recent call last):
  File "transformers/utils/import_utils.py", line 2073, in _get_module
  File "importlib/__init__.py", line 90, in import_module
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  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 "transformers/pipelines/__init__.py", line 27, in <module>
  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 "transformers/models/__init__.py", line 341, in <module>
  File "transformers/utils/import_utils.py", line 2597, in define_import_structure
  File "transformers/utils/import_utils.py", line 2308, in create_import_structure_from_path
FileNotFoundError: [Errno 2] No such file or directory: './dist/Sumnote/_internal/transformers/models/__init__.pyc'

The above exception was the direct cause of the following exception:
...
  File "transformers/utils/import_utils.py", line 2048, in __getattr__
    raise ModuleNotFoundError(
ModuleNotFoundError: Could not import module 'pipeline'. Are this object's requirements defined correctly?

Could somebody help me for this case?



bwoodsend

unread,
Jun 3, 2025, 4:15:43 PMJun 3
to PyInstaller

See #9134. It should be fixed in pyinstaller 6.14.0 which I released about 5 minutes ago.

Han

unread,
Jun 3, 2025, 6:30:23 PMJun 3
to PyInstaller
Thanks bwoodsend! I will check it.
Reply all
Reply to author
Forward
0 new messages