"No module named torch" in VSCode

Hi, everyone!

I know someone created a similar topic but it remained unsolved. I installed PyTorch last week through the ‘conda install pytorch torchvision -c pytorch’ command on the terminal in my Mac and it worked for a few days. This week it’s not working anymore, with the message “No module named Torch” on the output screen.

I’m in conda’s environment (as you can see in the picture) and I’ve tried all the possible environments. I also reinstalled PyTorch through the terminal and I installed PyTorch Snippets and it still doesn’t work…

Does anyone know what might be the problem?

Thanks in advance!

I see the problem is also happening with TensorFlow Snippets extension…

It seems like you didn’t properly setup the python interpreter path (for your situation, it is something like <path to anaconda>/env/pytorch/bin/python).
I would recommend to use the integrated terminal (Win: ctrl + j, MacOS: command + j) of VS Code to run the script, where you can activate your conda environment with conda activate pytorch.

Thanks for the answer!

It works on the integrated terminal, so I can use it, but on the VS Code editor it still doesn’t work. I’m attaching the pics of the environment I am in ~/opt/anaconda3/envs/pytorch/bin/python (which should be the same as in the terminal), and still raises the “No module named torch” error.

Thanks again David!

Captura de pantalla 2021-09-07 a les 17.12.53

Can you give more details how you “run in vscode editor”? Is it a feature of the “pytorch snippet” extension?

EDIT: maybe you could open your settings.json (command+,, then click the button on the top-right corner), just make sure that the default python path is the right one.

Yes, now I see the problem is in running the code from the editor to the output screen. When I run it from the editor to the terminal it works.

So I click at the top right button “Run Python file in Terminal” and it works, but when I click “Run code” so I have it on the output screen it shows the message “No module named torch”.

I see. It should be the problem of extension ‘code runner’, can you confirm you have this installed?
If is, check this link and setup your python path GitHub - formulahendry/vscode-code-runner: Code Runner for Visual Studio Code

Yes I have it. I think I don’t know how to write the path in my mac because now the output is as in the following picture (and the path I wrote is next):

"code-runner.executorMap": {
        "javascript": "node",
        "java": "cd $dir && javac $fileName && java $fileNameWithoutExt",
        "c": "cd $dir && gcc $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "cpp": "cd $dir && g++ $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "objective-c": "cd $dir && gcc -framework Cocoa $fileName -o $fileNameWithoutExt && $dir$fileNameWithoutExt",
        "php": "php",
        "python": "\\Users\\Gerard1\\opt\\anaconda3\\envs\\pytorch\\bin\\python",

Open the terminal and activate your conda environment, then run which python, just paste the ouput to python path

Nevermind, it works! It was the back slashes not working.

Now, may I ask how can I use other extensions in this environment? Such as tensorflow, matplotlib, etc?

Thanks again for all, David.

Do you mean VS Code extensions or python modules?
I think you don’t need extra extensions to use tensorflow or matplotlib. You could install them via conda or pip just like how you installed pytorch.