Unable to import torch in visual studios

good day to everyone,
im new to using pytorch so bare with me , i downloaded pytorch using pip and conda , and both the gpu and the cpu versions but i keep getting the same error using visual studio which is :
PS C:\Users\moham> & C:/Users/moham/AppData/Local/Microsoft/WindowsApps/python3.10.exe c:/Users/moham/Desktop/thesis/QNetwork.py
Traceback (most recent call last):
File “c:\Users\moham\Desktop\thesis\QNetwork.py”, line 1, in
import torch
File “C:\Users\moham\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torch_init_.py”, line 141, in
raise err
OSError: [WinError 126] The specified module could not be found. Error loading “C:\Users\moham\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torch\lib\shm.dll” or one of its dependencies.
PS C:\Users\moham> & C:/Users/moham/AppData/Local/Microsoft/WindowsApps/python3.10.exe c:/Users/moham/Desktop/thesis/QNetwork.py
Traceback (most recent call last):
File “c:\Users\moham\Desktop\thesis\QNetwork.py”, line 1, in
import torch
File “C:\Users\moham\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torch_init_.py”, line 141, in
raise err
OSError: [WinError 126] The specified module could not be found. Error loading “C:\Users\moham\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\torch\lib\shm.dll” or one of its dependencies.

i tried EVERYTHING on the internet and even put the path manually in the environment variables but i keep getting this error my python version is 3.10.11 and here is my code for context :
import torch
import torch.nn as nn
import torch.nn.functional as F

class QNetwork(nn.Module):
def init(self, input_size, output_size):
super(QNetwork, self).init()
self.fc1 = nn.Linear(input_size, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, output_size)

def forward(self, x):
    x = F.relu(self.fc1(x))
    x = F.relu(self.fc2(x))
    x = self.fc3(x)
    return x

Same issue as described here. Apply the suggested workaround from the GitHub issue or install the latest nightly binary.

1 Like

hey thank you , simply downloading pytorch 2.2 solved the issue