How to Fix "No Module Named torch"

Install PyTorch: pip install torch torchvision. For CUDA GPU support: pip install torch --index-url https://download.pytorch.org/whl/cu121

CPU Only (All Platforms)

pip install torch torchvision torchaudio

With CUDA GPU Support

# CUDA 12.1 (recommended for most modern GPUs)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu121

# CUDA 11.8 (older GPUs)
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

With Conda

# CPU only
conda install pytorch torchvision torchaudio cpuonly -c pytorch

# CUDA 12.1
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia

Common Issues

Wrong Python version

# Make sure pip matches your Python
python3 -m pip install torch
# or
python -m pip install torch

Virtual environment not activated

# Activate your venv first
source venv/bin/activate  # Linux/Mac
venv\Scripts\activate     # Windows
pip install torch

Verify installation

python -c "import torch; print(torch.__version__)"
# Should print something like: 2.3.0

# Check CUDA availability
python -c "import torch; print(torch.cuda.is_available())"

Related Questions

Try HeyTensor Shape Calculator