PyTorch: ModuleNotFoundError: No module named transformers after PyTorch Installation
This error occurs when Python tries to import transformers (e.g. from transformers import pipeline) without installing HuggingFace transformers in the active virtual environment. Run pip install transformers accelerate.
Root Cause Analysis
This error occurs when Python code or LLM inference scripts execute from transformers import pipeline or import transformers, but the HuggingFace transformers package is not present in the active Python environment's site-packages.
Root Cause 1: Transformers Is a Separate Package from PyTorch
PyTorch and HuggingFace Transformers are distinct independent libraries. Installing PyTorch (pip install torch) does not automatically install transformers.
Root Cause 2: Separate Virtual Environments Between Training and Inference
Developers frequently install transformers in their base Anaconda environment but create a fresh virtual environment for CUDA PyTorch without reinstalling high-level model libraries.
Root Cause 3: Missing Companion Packages (accelerate, tokenizers)
Modern HuggingFace pipelines often require accelerate for multi-GPU inference and device mapping (device_map='auto'). Omitting these complementary dependencies triggers downstream import errors.
Root Cause 4: Confusing Local Directory with Package Name
Creating a local directory or file named transformers.py shadows the HuggingFace package namespace.
Reproduction Code (MCVE)
import sys
# Simulating missing transformers package in active environment
def check_transformers_installed():
if "transformers" not in sys.modules and not any("transformers" in p for p in sys.path):
raise ModuleNotFoundError(
"ModuleNotFoundError: No module named 'transformers'. "
"HuggingFace Transformers is not included in PyTorch by default. "
"Please install it via: pip install transformers accelerate"
)
check_transformers_installed()
Solution 1: Install Transformers and Accelerate via pip
Install transformers, accelerate, and datasets in the active environment using pip.
# Solution 1: Terminal installation command
cmd = "pip install transformers accelerate safetensors"
print("Command to execute in terminal:")
print(f" $ {cmd}")
assert "transformers" in cmd
Solution 2: Mock HuggingFace Pipeline Pattern for Testing
Use a clean abstraction wrapper to handle optional transformers imports in modular applications.
# Solution 2: Safe pipeline initialization wrapper
class MockPipelineWrapper:
def __init__(self, task: str):
self.task = task
def __call__(self, text: str):
return [{"label": "POSITIVE", "score": 0.99}]
pipe = MockPipelineWrapper("sentiment-analysis")
result = pipe("PyTorch with Transformers is powerful.")
print("Pipeline result:", result)
assert result[0]["label"] == "POSITIVE"
When installing transformers for local LLM inference (e.g. Llama 3, Mistral), always install accelerate (pip install transformers accelerate) as well. Without accelerate, specifying device_map='auto' or torch_dtype=torch.bfloat16 in AutoModelForCausalLM.from_pretrained() will raise an ImportError: Using device_map='auto' requires accelerate.
Note de reproductibilité : Cette erreur dépend de l'état d'isolation de l'environnement virtuel Python et de la présence du paquet transformers dans site-packages.
Contrast transformers with torch: torch provides core tensor computation, autograd, and neural network primitives; transformers provides pre-trained weights, tokenizers, and model architectures built on top of PyTorch.