Home › Python

ModuleNotFoundError: No module named 'torch' (Python) — how to fix

Traceback (most recent call last):
  File "train.py", line 1, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'

Quick fix: Install with the interpreter that runs the code: python -m pip install torch (add torchvision torchaudio if needed). For a specific CUDA build use the command from pytorch.org, e.g. python -m pip install torch --index-url https://download.pytorch.org/whl/cu124. In Jupyter use %pip install torch and restart the kernel.

What it means

Python found no package named torch on sys.path. Either PyTorch isn't installed in this environment, or it went into a different Python, conda env or Jupyter kernel. Check which interpreter runs your code:

python -c "import sys; print(sys.executable)"

The import is torch. On PyPI the package is also called torch. pip install pytorch is wrong: that name is a placeholder that doesn't install PyTorch.

Fix 1: install with python -m pip

CPU or default build (on Linux the default PyPI wheel includes CUDA support):

python -m pip install torch torchvision torchaudio

For a specific CUDA version or a CPU-only build, use the selector on pytorch.org. It gives a command with an index URL:

# CPU only (much smaller download)
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
# CUDA 12.4 build
python -m pip install torch --index-url https://download.pytorch.org/whl/cu124

Verify:

python -c "import torch; print(torch.__version__, torch.cuda.is_available())"

Fix 2: "No matching distribution found for torch"

pip finds no wheel for your setup. Common reasons:

See Could not find a version that satisfies the requirement.

Fix 3: conda environments

Activate the environment before installing and before running:

conda create -n ml python=3.11
conda activate ml
python -m pip install torch torchvision

PyTorch no longer publishes new releases to its own pytorch conda channel, so pip inside the conda env is the recommended way. Avoid mixing two copies of torch (one from conda, one from pip) in one env.

Fix 4: Jupyter, VS Code, Colab

Fix 5: it imports in the terminal but not in your script

That's not this error, but it often follows. You installed the CPU build, or the NVIDIA driver is missing or too old. Reinstall with the CUDA index URL from Fix 1 and check nvidia-smi.

Last updated 2026-11-15.