transformers - ✅(Solved) Fix import transformers takes long sometimes [1 pull requests, 11 comments, 4 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#44246Fetched 2026-04-08 00:29:37
View on GitHub
Comments
11
Participants
4
Timeline
27
Reactions
0
Timeline (top)
commented ×11subscribed ×7mentioned ×6closed ×1

Fix Action

Fixed

PR fix notes

PR #44253: Speed create_import_structure up with os.scandir()

Description (problem / solution / changelog)

create_import_structure_from_path does some redundant os calls, so I'm experimenting with changes to see if we can speed up loading a lot.

Related to #44246

Changed files

  • src/transformers/utils/import_utils.py (modified, +9/-15)

Code Example

from datetime import datetime
print("Before importing files...")
start = datetime.now()
import transformers
delta = datetime.now() - start
print(f"Import done: {delta.seconds}.{delta.microseconds} seconds")

---

Before importing files...
PyTorch was not found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Import done: 10.512374 seconds

---

Before importing files...
PyTorch was not found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Import done: 0.836140 seconds
RAW_BUFFERClick to expand / collapse

System Info

  • transformers version: 5.2.0
  • Platform: macOS-15.7.3-arm64-arm-64bit-Mach-O
  • Python version: 3.13.11
  • Huggingface_hub version: 1.4.1
  • Safetensors version: 0.7.0
  • Accelerate version: not installed
  • Accelerate config: not found
  • DeepSpeed version: not installed
  • PyTorch version (accelerator?): not installed (NA)
  • Using distributed or parallel set-up in script?: Unknown

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, ...)
  • My own task or dataset (give details below)

Reproduction

Use the following script:

from datetime import datetime
print("Before importing files...")
start = datetime.now()
import transformers
delta = datetime.now() - start
print(f"Import done: {delta.seconds}.{delta.microseconds} seconds")

You get:

Before importing files...
PyTorch was not found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Import done: 10.512374 seconds

Expected behavior

Relatively small import time:

Before importing files...
PyTorch was not found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Import done: 0.836140 seconds

extent analysis

Fix Plan

Install PyTorch

The root cause of the issue is the missing PyTorch installation. To fix this, we need to install PyTorch.

Step-by-Step Installation

  1. Install PyTorch using pip:

pip install torch torchvision

2. **Verify the installation**:
   ```bash
python -c "import torch; print(torch.__version__)"

This should print the version of PyTorch installed.

Update transformers Installation

After installing PyTorch, we need to update the transformers installation to use the newly installed PyTorch.

Step-by-Step Update

  1. Uninstall the existing transformers installation:

pip uninstall transformers

2. **Reinstall `transformers`**:
   ```bash
pip install transformers
  1. Verify the installation:

python -c "import transformers; print(transformers.version)"

   This should print the version of `transformers` installed.

### Verify the Fix

Run the original script again to verify that the import time has decreased:
```python
from datetime import datetime
print("Before importing files...")
start = datetime.now()
import transformers
delta = datetime.now() - start
print(f"Import done: {delta.seconds}.{delta.microseconds} seconds")

This should print an import time similar to the expected behavior.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

Relatively small import time:

Before importing files...
PyTorch was not found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.
Import done: 0.836140 seconds

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

transformers - ✅(Solved) Fix import transformers takes long sometimes [1 pull requests, 11 comments, 4 participants]