Unable to import torch after update no module torch._utils

Hello, I recently attempted to update my torch setup and have encountered a strange issue.

I have been using torch through WSL to access ROCm on my AMD graphics card. I have torch installed in a Python 3.10 virtual environment on my Ubuntu 22.04 distribution. Recently, AMD released a new driver which I decided to update to and this required me to update to torch 2.3.0. After following their update procedure I was unable to import torch in either script or interactive python sessions. The import would fail with the following error message:

...sdenv310/lib/python3.10/site-packages/torch/__init__.py", line 27, in <module>
    from ._utils import _import_dotted_name, classproperty
ModuleNotFoundError: No module named 'torch._utils'

To test if this was an issue with the torch wheel on AMD’s site I tried installing directly from PyTorch instead but I get the same result and the same thing happens when I try 2.5

Any idea what could be interfering with it and stopping it from finding _utils?

To add to this: I uninstalled from the virtual environment and repeated the install procedure found here: Install PyTorch for ROCm — Use ROCm on Radeon GPUs for the WSL-specific distribution of PyTorch+ROCm in the global environment instead.

It now works but only in the global environment . My virtual environment is configured to load packages from the global environment, I can see torch in the pip list in the virtual environment, but trying to import torch in the virtual environment still results in the same “ModuleNotFoundError”, while working perfectly in the global environment. I don’t know what to make of this.

Turned out to be an issue with pip uninstall not cleaning things up correctly for some reason. After calling pip uninstall for all torch-related packages again , both inside and outside of the virtual environment, I was able to verify that nothing was left behind. Reinstalling in the virtual environment worked after that.