ModuleNotFoundError: No module named 'transformers' (Python) — how to fix
Traceback (most recent call last):
File "app.py", line 1, in <module>
from transformers import AutoTokenizer, AutoModelForCausalLM
ModuleNotFoundError: No module named 'transformers'
Quick fix: Install it into the interpreter you actually run: python -m pip install transformers (plus a backend such as python -m pip install torch). In Jupyter use %pip install transformers and restart the kernel. Check with python -c "import sys, transformers; print(sys.executable, transformers.__version__)".
What it means
Python searched every directory on sys.path and found no package called transformers. The PyPI package and the import name are the same (transformers), so this is almost never a naming problem: the package is missing from this interpreter.
Cause 1: not installed at all
python -m pip install transformers
transformers needs a deep learning backend to load models. Most people want PyTorch:
python -m pip install "transformers[torch]"
# or separately
python -m pip install transformers torch
Without a backend the import succeeds, but loading a model prints None of PyTorch, TensorFlow >= 2.0, or Flax have been found.
Cause 2: installed into a different Python
pip install and python can point to different interpreters (system Python, a venv, conda, pyenv). Always install with python -m pip using the same python that runs your script, and check:
which python # Windows: where python
python -m pip show transformers
python -c "import sys; print(sys.executable)"
If you use a virtual environment, activate it first:
python -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install transformers torch
On Debian/Ubuntu, installing into system Python fails with externally-managed-environment. Use a venv as above rather than --break-system-packages.
Cause 3: Jupyter / Colab uses another kernel
A notebook kernel often isn't the Python in your terminal. Install from inside the notebook so it goes to the kernel's interpreter:
%pip install transformers torch
Then restart the kernel (Kernel → Restart). !pip install may install into the wrong environment; %pip does not. In VS Code, check the interpreter selected at the top right of the notebook.
Cause 4: conda environment not active
conda activate myenv
conda install -c conda-forge transformers
# or
python -m pip install transformers
Packages installed in base aren't visible in other envs and vice versa.
Cause 5: a local file named transformers.py
If your project has a transformers.py file or a transformers/ folder, Python may import that instead, or fail inside it. The error then often becomes ImportError: cannot import name 'AutoTokenizer' from 'transformers'. Rename the file and delete any __pycache__:
python -c "import transformers; print(transformers.__file__)"
The path should end in site-packages/transformers/__init__.py.
Cause 6: old version missing a class
If the module imports but a specific name fails (cannot import name 'AutoModelForImageTextToText'), your version is too old. Upgrade:
python -m pip install -U transformers
Verify
python -c "import transformers, torch; print(transformers.__version__, torch.__version__)"
Last updated 2026-11-18.