ModuleNotFoundError: No module named 'torch' (tested fix)
ModuleNotFoundError: No module named 'torch' almost always means torch is installed for a different Python than the one running your code. Below the same script fails under system python3 and works in the venv; the fix is python -m pip install torch for the interpreter you run, and never pip install pytorch.
1. Reproduce: same script, two interpreters
torch 2.14.1+cpu is installed in .venv only. b1.py prints which Python is running and tries the import:
import sys
print("python:", sys.executable)
try:
import torch
print("torch", torch.__version__, "from", torch.__file__.split("site-packages")[0] + "site-packages")
except ModuleNotFoundError as e:
print(type(e).__name__ + ":", e)
$ python3 b1.py
python: /usr/bin/python3
ModuleNotFoundError: No module named 'torch'
$ .venv/bin/python b1.py
python: /tmp/nt421/.venv/bin/python
torch 2.14.1+cpu from /tmp/nt421/.venv/lib/python3.13/site-packages
That is the cause almost every time: torch is installed, but into a different Python than the one running your code (system python3, another venv or conda env, or a Jupyter/VS Code kernel pointing elsewhere).
2. Fix: install into the interpreter you actually run
python -c "import sys; print(sys.executable)" # the Python your code uses
python -m pip install torch # install into exactly that one
# CPU-only wheel (much smaller):
python -m pip install torch --index-url https://download.pytorch.org/whl/cpu
- Use
python -m pip, not barepip: barepipcan belong to a different Python onPATH. - Jupyter / VS Code: run
import sys; print(sys.executable)in a notebook cell and install with%pip install torch(it targets the running kernel), or switch the kernel/interpreter to the venv that has torch. - If pip says
No matching distribution found for torch, your Python version or platform has no wheel: see pip install torch: no matching distribution found.
3. Do not pip install pytorch
The PyPI package pytorch (1.0.2, a 689-byte sdist) is a placeholder that fails on purpose:
$ pip install pytorch
Building wheels for collected packages: pytorch
Building wheel for pytorch (pyproject.toml): started
Building wheel for pytorch (pyproject.toml): finished with status 'error'
error: subprocess-exited-with-error
× Building wheel for pytorch (pyproject.toml) did not run successfully.
[... 20 traceback lines trimmed ...]
Exception: You tried to install "pytorch". The package named for PyTorch is "torch"
[end of output]
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed building wheel for pytorch
Failed to build pytorch
error: failed-wheel-build-for-install
× Failed to build installable wheels for some pyproject.toml based projects
╰─> pytorch
exit 1
The package name is torch; the import name is also torch. (Run with pip 26.2.2 on Python 3.11; output trimmed where marked.)
Related: Torch not compiled with CUDA enabled, RuntimeError: Numpy is not available, No module named 'requests'.
Frequently asked questions
Why do I get No module named 'torch' after installing it?
It was installed into a different Python environment. Print sys.executable and install with that interpreter: python -m pip install torch.
Is the pip package called pytorch or torch?
torch. The PyPI package named pytorch is a placeholder that fails with: You tried to install "pytorch". The package named for PyTorch is "torch".
How do I fix No module named 'torch' in Jupyter?
Install into the running kernel with %pip install torch, or switch the notebook kernel to the environment that has torch.
Written with AI assistance. Measured on 2026-10-24 on Linux: torch 2.14.1+cpu in a Python 3.13.5 venv vs system Python 3.13.5, and pip 26.2.2 on Python 3.11 for pip install pytorch. Output pasted unedited except where marked as trimmed. PlainTables is our extension; PyTorch is free.