PyTorch: ModuleNotFoundError: No module named torch in Python
This error occurs when Python tries to execute import torch in an environment where PyTorch is not installed. Install PyTorch from the official channel via pip install torch.
Root Cause Analysis
This error occurs when Python executes a script containing import torch or from torch import nn, but the active interpreter cannot locate the torch package in sys.path.
Root Cause 1: PyTorch Not Installed in Active Virtual Environment
The virtual environment currently executing the script has not had torch installed into its site-packages.
Root Cause 2: IDE Using Wrong Python Interpreter
In VS Code or PyCharm, the selected interpreter in the editor settings points to a global Python installation where PyTorch is absent, while the terminal uses a different environment.
Root Cause 3: Incompatible Python Version
PyTorch wheels are released for specific Python versions (e.g. 3.9 through 3.12). Attempting to install PyTorch on an unsupported pre-release version of Python (such as Python 3.13 alpha/beta) results in pip failing to find a compatible wheel.
Root Cause 4: Typo in Package Name During pip Install
Users sometimes run pip install pytorch instead of pip install torch. On PyPI, the official package name is torch (the pytorch package on PyPI is an unmaintained legacy placeholder).
Reproduction Code (MCVE)
import sys
# Simulating missing torch module in environment
def check_torch_installed():
if "torch" not in sys.modules and not any("torch" in p for p in sys.path):
raise ModuleNotFoundError(
"ModuleNotFoundError: No module named 'torch'. "
f"Active interpreter: {sys.executable}. "
"Please install PyTorch using: pip install torch"
)
check_torch_installed()
Solution 1: Install Official PyTorch Package via pip
Install the official torch package using python -m pip install torch.
import sys
# Solution 1: Terminal install command for active interpreter
install_cmd = f'"{sys.executable}" -m pip install torch torchvision torchaudio'
print("Execute the following command:")
print(f" $ {install_cmd}")
assert "pip install torch" in install_cmd
Solution 2: Select Correct Python Interpreter in IDE
Configure your IDE (VS Code / PyCharm) to use the project's .venv interpreter.
# Solution 2: Interpreter selection reminder
steps = [
"Open VS Code Command Palette: Ctrl+Shift+P (or Cmd+Shift+P on macOS)",
"Search for: 'Python: Select Interpreter'",
"Select your project's .venv path (e.g. .venv/bin/python or .venv\\Scripts\\python.exe)"
]
print("IDE configuration instructions:")
for s in steps:
print(f" - {s}")
assert len(steps) == 3
Never run pip install pytorch. The official distribution on PyPI is torch. Running pip install pytorch installs an outdated package from 2018. Always run pip install torch or visit pytorch.org for custom CUDA wheel commands.
Note de reproductibilité : Cette erreur dépend de l'état de l'environnement virtuel et de la sélection de l'exécutable Python actif.
Contrast pip install torch with pip install pytorch-lightning: torch is the core deep learning engine; pytorch-lightning is a high-level training framework built on top of PyTorch.