"No module named torch" in VSCode

username: Reddie23
cooked: <p>Hi, everyone!</p>
<p>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.</p>
<p>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…</p>
<p>Does anyone know what might be the problem?</p>
<p>Thanks in advance!</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/1/516351ceeefe8b51dcd2570e435f9202a7a2d3ec.png" data-download-href="https://discuss.pytorch.org/uploads/default/516351ceeefe8b51dcd2570e435f9202a7a2d3ec" title="Captura de pantalla 2021-09-06 a les 13.10.23"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/1/516351ceeefe8b51dcd2570e435f9202a7a2d3ec_2_690x388.png" alt="Captura de pantalla 2021-09-06 a les 13.10.23" data-base62-sha1="bBZsUq4hzbBaButVdYyuhjVj3VW" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/1/516351ceeefe8b51dcd2570e435f9202a7a2d3ec_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/1/516351ceeefe8b51dcd2570e435f9202a7a2d3ec_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/5/1/516351ceeefe8b51dcd2570e435f9202a7a2d3ec_2_1380x776.png 2x" data-dominant-color="161A1A"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Captura de pantalla 2021-09-06 a les 13.10.23</span><span class="informations">1920×1080 165 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: Reddie23
cooked: <p>I see the problem is also happening with TensorFlow Snippets extension…</p>

username: huahuanZ
cooked: <p>It seems like you didn’t properly setup the python interpreter path (for your situation, it is something like <code>&lt;path to anaconda&gt;/env/pytorch/bin/python</code>).<br>
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 <code>conda activate pytorch</code>.</p>

username: Reddie23
cooked: <p>Thanks for the answer!</p>
<p>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 <code>~/opt/anaconda3/envs/pytorch/bin/python</code> (which should be the same as in the terminal), and still raises the “No module named torch” error.</p>
<p>Thanks again David!</p>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/5/2/52b1efa098de50fda655ca1953dbba90ed4c4d93.png" alt="Captura de pantalla 2021-09-07 a les 17.12.53" data-base62-sha1="bNynu55AkmvZujnDBdrsQujoruP" width="596" height="311"></p>

username: Reddie23
cooked: <p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/c/bcf6b3c57732cc108f434dc171cd461c42dcee18.png" data-download-href="https://discuss.pytorch.org/uploads/default/bcf6b3c57732cc108f434dc171cd461c42dcee18" title="Captura de pantalla 2021-09-07 a les 17.12.18"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bcf6b3c57732cc108f434dc171cd461c42dcee18_2_690x120.png" alt="Captura de pantalla 2021-09-07 a les 17.12.18" data-base62-sha1="qXEjVZCcwQ9YG8WkPZqfoivfkJG" width="690" height="120" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bcf6b3c57732cc108f434dc171cd461c42dcee18_2_690x120.png, https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bcf6b3c57732cc108f434dc171cd461c42dcee18_2_1035x180.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/b/c/bcf6b3c57732cc108f434dc171cd461c42dcee18_2_1380x240.png 2x" data-dominant-color="191D1D"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Captura de pantalla 2021-09-07 a les 17.12.18</span><span class="informations">1495×262 27.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: huahuanZ
cooked: <p>Can you give more details how you “run in vscode editor”? Is it a feature of the “pytorch snippet” extension?</p>
<p>EDIT: maybe you could open your <code>settings.json</code> (<code>command</code>+<code>,</code>, then click the button on the top-right corner), just make sure that the default python path is the right one.</p>

username: Reddie23
cooked: <p>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.</p>
<p>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”.</p>

username: huahuanZ
cooked: <p>I see. It should be the problem of extension ‘code runner’, can you confirm you have this installed?<br>
If is, check this link and setup your python path <a href="https://github.com/formulahendry/vscode-code-runner#configuration" class="inline-onebox" rel="noopener nofollow ugc">GitHub - formulahendry/vscode-code-runner: Code Runner for Visual Studio Code</a></p>

username: Reddie23
cooked: <p>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):</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/8/d/8d32c66b02a1f10873611ca0a5adce998c8e717d.png" data-download-href="https://discuss.pytorch.org/uploads/default/8d32c66b02a1f10873611ca0a5adce998c8e717d" title="Captura de pantalla 2021-09-07 a les 18.26.17"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/8/d/8d32c66b02a1f10873611ca0a5adce998c8e717d_2_690x394.png" alt="Captura de pantalla 2021-09-07 a les 18.26.17" data-base62-sha1="k966ILtKAHv0Agb8qWa0mSD4Zad" width="690" height="394" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/8/d/8d32c66b02a1f10873611ca0a5adce998c8e717d_2_690x394.png, https://discuss.pytorch.org/uploads/default/optimized/3X/8/d/8d32c66b02a1f10873611ca0a5adce998c8e717d_2_1035x591.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/8/d/8d32c66b02a1f10873611ca0a5adce998c8e717d_2_1380x788.png 2x" data-dominant-color="151919"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Captura de pantalla 2021-09-07 a les 18.26.17</span><span class="informations">1591×910 38.5 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<pre><code class="lang-auto">"code-runner.executorMap": {
        "javascript": "node",
        "java": "cd $dir &amp;&amp; javac $fileName &amp;&amp; java $fileNameWithoutExt",
        "c": "cd $dir &amp;&amp; gcc $fileName -o $fileNameWithoutExt &amp;&amp; $dir$fileNameWithoutExt",
        "cpp": "cd $dir &amp;&amp; g++ $fileName -o $fileNameWithoutExt &amp;&amp; $dir$fileNameWithoutExt",
        "objective-c": "cd $dir &amp;&amp; gcc -framework Cocoa $fileName -o $fileNameWithoutExt &amp;&amp; $dir$fileNameWithoutExt",
        "php": "php",
        "python": "\\Users\\Gerard1\\opt\\anaconda3\\envs\\pytorch\\bin\\python",
</code></pre>

username: huahuanZ
cooked: <p>Open the terminal and activate your conda environment, then run <code>which python</code>, just paste the ouput to python path</p>

username: Reddie23
cooked: <p>Nevermind, it works! It was the back slashes not working.</p>
<p>Now, may I ask how can I use other extensions in this environment? Such as tensorflow, matplotlib, etc?</p>
<p>Thanks again for all, David.</p>

username: huahuanZ
cooked: <p>Do you mean VS Code extensions or python modules?<br>
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.</p>