ModuleNotFoundError: No module named 'torch'
The Python that ran your script does not have PyTorch installed. Almost always you installed into a different Python than the one you are using.
Updated
The error
Traceback (most recent call last):
File "train.py", line 1, in <module>
import torch
ModuleNotFoundError: No module named 'torch'The same shape of error appears for every package — No module named 'sklearn', 'transformers', 'cv2', 'pandas'. Everything below applies to all of them.
What it means
Python looked through every folder on its import path and found nothing called torch. That is all it knows. It does not mean the installation failed, and it does not mean the package is broken.
In the large majority of cases, PyTorch is installed on your machine — inside a different Python than the one that ran this script.
Why it happens
A computer running data science work usually has several Pythons on it: the system Python, one from python.org, one from Anaconda, and one inside each project's virtual environment. Each has its own separate site-packages folder. A package installed into one is invisible to all the others.
The mismatch happens in a few reliable ways.
You ran pip install torch in one terminal and python train.py in another where the virtual environment was never activated. Or pip on your PATH belongs to a different Python than python does — very common on macOS and Linux where pip may point at the system Python. Or you installed from a terminal but are running the code in a Jupyter notebook, and the notebook's kernel is a different environment entirely. Or the install genuinely failed and the error scrolled past above a lot of other output.
How to fix it
1. Find out which Python is actually running your code, then install into that one. Put these two lines at the top of the failing script, or run them in the failing notebook cell:
import sys
print(sys.executable)
print(sys.path)Now install using that exact interpreter. The -m pip form is the important part — it guarantees the install goes to the Python you named, with no PATH guesswork:
# use the path that sys.executable printed
/home/you/venvs/ai/bin/python -m pip install torch# Windows
C:\Users\you\venvs\ai\Scripts\python.exe -m pip install torchMake python -m pip install your default habit instead of bare pip install. It removes this entire category of problem.
2. Check the virtual environment is activated in the terminal you are running from. An activated environment shows its name in the prompt, like (ai) you@machine:~$.
# macOS / Linux
source .venv/bin/activate
python -m pip install torch
python train.py# Windows PowerShell
.\.venv\Scripts\Activate.ps1
python -m pip install torch
python train.pyIf PowerShell refuses to run the activation script, that is an execution-policy block, fixed for the current session with Set-ExecutionPolicy -Scope Process -ExecutionPolicy RemoteSigned.
3. If this is a Jupyter or Colab notebook, check the kernel. Run this in a cell:
import sys
print(sys.executable)If that path is not your project environment, the kernel is wrong. Either pick the right kernel from the Kernel menu, or register your environment as a kernel:
source .venv/bin/activate
python -m pip install ipykernel
python -m ipykernel install --user --name=ai-project --display-name "Python (ai-project)"Then select "Python (ai-project)" in the notebook. As an escape hatch inside any notebook, %pip install torch installs into the kernel that is running, which is what !pip install fails to guarantee.
4. Install the build that matches your machine. PyTorch ships different wheels for CPU-only and for each CUDA version, and picking the wrong index URL is a frequent cause of an install that appears to work and then does not.
# CPU only — works everywhere, including machines with no NVIDIA GPU
python -m pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpuFor a GPU machine, take the exact command from the selector at pytorch.org, because the CUDA tag in the URL changes between releases.
5. Read the install output if it still fails. Two messages have specific meanings. Could not find a version that satisfies the requirement torch usually means your Python version is newer than the wheels available, or you are on 32-bit Python, or on a platform without a prebuilt wheel — check python --version and try a well-supported version such as 3.11 or 3.12. No space left on device means exactly that; PyTorch with CUDA needs several gigabytes.
6. Confirm the install landed where you think.
python -m pip show torchThe Location: line tells you which site-packages received it. If that path does not sit inside the environment your sys.executable reported, you have found the problem.
How to prevent it
One virtual environment per project, created with the interpreter you intend to use, and activated before you touch anything:
python -m venv .venv
source .venv/bin/activate # Windows: .\.venv\Scripts\Activate.ps1
python -m pip install --upgrade pip
python -m pip install torch torchvision
python -m pip freeze > requirements.txtThree habits do the rest. Always write python -m pip, never bare pip. Keep requirements.txt in the repository so a fresh machine reproduces the environment in one command. And in editors like VS Code, set the interpreter explicitly for the workspace so the Run button and the integrated terminal agree with each other.
One last trap worth knowing, because it produces confusing symptoms rather than this clean error: never name your own file after a library. A file called torch.py, random.py or email.py in your working directory will be imported instead of the real package, and the failure looks like a missing attribute rather than a missing module.
Related errors
- torch.cuda.is_available() returns False — torch imports fine but has no GPU support, which is the CPU wheel doing its job
- CUDA out of memory
- DataLoader worker is killed by signal