ImportError: transformers requires the PyTorch library but it was not found in your environment
Resolves the transformers backend-missing ImportError by installing torch/tensorflow/flax into the right interpreter. Use when the error names the missing backend library. Not for No module named 'transformers' itself.
TL;DR: pip install torch (or tensorflow/flax, pick your backend). transformers needs at least one deep learning backend at import time. The pip install of transformers alone does not pull in torch. If torch is installed, you have an environment mismatch, install it with the same python you run.
The error:
ImportError: transformers requires the PyTorch library but it was not found in your environment. Please install torch or tensorflow or flax.Fix
- Run
python -c "import torch"
Expected: ImportError confirms torch is missing from this interpreter
- Install:
python -m pip install torch(CPU) or the CUDA wheel from pytorch.org
Expected: Successfully installed torch
- Re-run your transformers import
Expected: no ImportError, transformers loads
When this applies
- the full error text about transformers requiring torch/tensorflow/flax
- you installed only
pip install transformers
When this does not apply
- No module named 'transformers' (install transformers itself)
- a specific name like AutoModel fails to import (different problem)
Version compatibility
transformers 4.x with any backend. Error raised by transformers' dependency check in init.
Why it happens
transformers lazily requires one of torch, tensorflow, or flax. The PyPI package declares them as optional extras, so a bare pip install transformers leaves you with none and the import guard fires.
Edge cases
pip install transformers[torch]pulls the backend in one step.- If you only need tokenizers, that subpackage imports fine without a backend.
- Same guard exists for tensorflow and flax variants of the message.
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.