ImportError: cannot import name 'cached_path' from 'transformers.file_utils'
Fixes ImportError: cannot import name 'cached_path' from 'transformers.file_utils' Use when importing a transformers-based library fails on `from transformers.file_utils import cached_path` Not for: you import cached_path in your own code (just import it from huggingface_hub instead)
How to fix ImportError: cannot import name 'cachedpath' from 'transformers.fileutils'
A library you use (txtai, classy-classification, etc.) imports cached_path from its old home and your transformers is too new. Upgrade that library; if it has no fixed release, pin transformers<=4.21.3. cached_path was removed from transformers.fileutils (it lives in huggingfacehub now) in late 2022.
The exact error
ImportError: cannot import name 'cached_path' from 'transformers.file_utils'Fix it
- Read the traceback to find the importing library (the frame above transformers).
Expected: e.g. txtai, classy_classification
- Upgrade it:
pip install -U [library].
Expected: the new release imports from the new location
- If no fixed release exists:
pip install "transformers<=4.21.3".
Expected: import works again
When this applies
importing a transformers-based library fails on from transformers.file_utils import cached_path
When this does NOT apply
you import cachedpath in your own code (just import it from huggingfacehub instead)
Versions
transformers >= 4.22 with libs importing the old path
Why it happens
transformers removed fileutils.cachedpath; the canonical home is now huggingface_hub.
Edge cases
Pinning transformers back is temporary; the library will break again on other new-transformers changes.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.