ImportError: cannot import name 'Tensor' from 'torch' (unknown location)

I’m trying to import Tensor from PyTorch, but I keep getting this error:

ImportError: cannot import name 'Tensor' from 'torch' (unknown location)

Here’s the code causing the issue:

from torch import Tensor

What I’ve Tried:

  1. Checked that PyTorch is installed (pip show torch), and I’m using version 2.5.1.
  2. Reinstalled PyTorch:
    pip uninstall torch
    pip install torch
    
  3. Tested the import in a Python shell, but the error persists.

Environment:

  • Python version: 3.10
  • PyTorch version: 2.5.1
  • OS: Windows 10
  • Virtual environment: Yes

How can I fix this issue?

Вернуться на верх