Version 4.52.3 leads to error after bundling with pyinstaller
Author: HiokKuek
Pull request: no
Labels: name: bug

### System Info

The new versioning induces the following error when trying to bundle a python script that contains imports from the transformers library. 
```
File "functions\classify.py", line 1, in <module>
    from transformers import DistilBertTokenizer, DistilBertForSequenceClassification
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1412, in _handle_fromlist
  File "transformers\utils\import_utils.py", line 2045, in __getattr__
  File "transformers\utils\import_utils.py", line 2075, in _get_module
  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\models\__init__.py", line 341, in <module>
  File "transformers\utils\import_utils.py", line 2594, in define_import_structure
  File "transformers\utils\import_utils.py", line 2305, in create_import_structure_from_path
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\ernest\\Desktop\\URL-Extractor-Extension\\native_messaging_genai\\dist\\main\\_internal\\transformers\\models\\__init__.pyc'
[PYI-9628:ERROR] Failed to execute script 'main' due to unhandled exception!

```

Error goes away after downgrading to v4.51.3

### Who can help?

_No response_

### Information

- [ ] The official example scripts
- [ ] My own modified scripts

### Tasks

- [ ] An officially supported task in the `examples` folder (such as GLUE/SQuAD, ...)
- [x] My own task or dataset (give details below)

### Reproduction

1. Python script that uses transformers, in my case `from transformers import AutoTokenizer, AutoModelForSequenceClassification` 
2. bundle with pyinstall 
3. run the .exe file 

### Expected behavior

The things you intend to do with your python script (native host application in my case). 

The recent update made it such that your application will crash wth the error as described above when you try and run the .exe. 

Comments:
Hmmn, I'm not sure we officially support Pyinstaller, and this error message doesn't give us a lot of information about what's happening!

Can you do a [git bisect](https://git-scm.com/docs/git-bisect) to find the commit that causes this issue? That might give us a starting point to investigate and fix it

I'm experiencing a very similar issue when using sentence-transformers and transformers together in a PyInstaller bundle. The error I get is:
```FileNotFoundError: [WinError 3] The system cannot find the path specified: '...\\_MEIxxxxx\\transformers\\models\\__init__.pyc'```

I'm using Anaconda on Windows, and already tried collect_data_files and collect_submodules in the .spec file — no luck so far.

I described the full problem and the steps I’ve tried on Stack Overflow:
[https://stackoverflow.com/questions/79643167/error-creating-pyinstaller-executable-with-sentence-transformers-model-and-anaco](https://stackoverflow.com/questions/79643167/error-creating-pyinstaller-executable-with-sentence-transformers-model-and-anaco)

Any suggestions or workarounds would be really appreciated. Thanks in advance!


You can try posting on the community support space, where someone from the community or one of the Hugging Face engineers might be able to help: https://huggingface.co/spaces/transformers-community/support

Still, the git bisect output would be quite helpful to us for tracking this one down!

Recently I have also experienced troubles building an app with transformers dependency (via sentence_transformers) using pyinstaller. I do not use Anaconda though.

Versions:

- pyinstaller               6.14.1
- pyinstaller-hooks-contrib 2025.5
- sentence-transformers     4.1.0
- transformers              4.52.4

Symptoms:

When running the app after calling _model = SentenceTransformer(...) I got this error:

    ModuleNotFoundError: No module named 'transformers.models.deepseek_v3'

Context:

I have exported the model to being used locally - not to be bundled using Pyinstaller:

    model = SentenceTransformer(
        model_name,
        truncate_dim=vector_len,
        device=device,
        local_files_only=True,
        trust_remote_code=False,
    )
    model.save(local_model_path)

The relative local_model_path being: 

     dist/instance/models/...

The app then loads the model from the dir when being run packed:

    ...
    global _model
    if _model is None:
        if getattr(sys, "frozen", False):
            local_model_path = resource_path(
                "instance/models/..."
            )
            _model = SentenceTransformer(
                local_model_path,
                truncate_dim=vector_len,
                device=device,
                local_files_only=True,
                trust_remote_code=False,
            )
        else:
            _model = SentenceTransformer(
                model_name,
                trust_remote_code=True,
                local_files_only=False,
                truncate_dim=vector_len,
                device=device,
            )
    return _model

Resolution:

After much fiddling I have used this workaround:

1. Copy the [hook-transformers.py](https://github.com/pyinstaller/pyinstaller-hooks-contrib/blob/master/_pyinstaller_hooks_contrib/stdhooks/hook-transformers.py) into the app subdir named: pyinstaller_hooks

2. Add this code at the end of the hook-transformers.py file:

        hiddenimports = []
        hiddenimports += [
            f"transformers.models.{name}"
            for _, name, is_pkg in pkgutil.iter_modules(transformers.models.__path__)
            if is_pkg
        ]

3. Run the build:

        pyinstaller.exe --clean --log-level ERROR --onefile --hidden-import=transformers --additional-hooks-dir=pyinstaller_hooks --distpath dist myapp.py

I hope this might help somebody ...



> I'm experiencing a very similar issue when using sentence-transformers and transformers together in a PyInstaller bundle. The error I get is: `FileNotFoundError: [WinError 3] The system cannot find the path specified: '...\\_MEIxxxxx\\transformers\\models\\__init__.pyc'`
> 
> I'm using Anaconda on Windows, and already tried collect_data_files and collect_submodules in the .spec file — no luck so far.
> 
> I described the full problem and the steps I’ve tried on Stack Overflow: https://stackoverflow.com/questions/79643167/error-creating-pyinstaller-executable-with-sentence-transformers-model-and-anaco
> 
> Any suggestions or workarounds would be really appreciated. Thanks in advance!

I faced the same issue here, downgrading transformers version to 4.48.3 resolved the problem

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

A note - `transformers` package has `models\__init__.py` file, but the `pyinstaller` compiled `.exe` tries load `models\__init__.pyc`. 

Update of `pyinstaller`, `pyinstaller-hooks-contrib` and `transformers` to the latest versions fixed the issue



@raivisdejus Can you tell us the python version along with version of pyinstaller, pyinstaller-hooks-contrib and transformers that you are using? Thanks!

python 3.12.3
pyinstaller               6.12.0
pyinstaller-hooks-contrib 2025.1
transformers              4.53.0