ModuleNotFoundError: No module named 'torch' (conda environment)

amyxlu: <p>I’ve double checked to ensure that the conda environment is activated. I redownloaded PyTorch using <code>conda install -c pytorch pytorch</code>. When I do <code>conda list</code> I can see <code>pytorch 1.0.1 py3.6_cuda10.0.130_cudnn7.4.2_2 pytorch</code> listed.</p>
<p>Not too sure what else I can try to figure out why I’m getting this error. Any help would be appreciated!</p>

ptrblck: <p>Do you get this error in the terminal or are you using any IDE?</p>

amyxlu: <p>Nope, not using an IDE. I’m running it directly from the terminal using <code>python script.py</code>.</p>

ptrblck: <p>Did you create a specific conda environment before installing PyTorch?</p>

amyxlu: <p>Yes, I did. I’d checked to make sure that the custom conda environment into which torch was installed was activated.</p>

ptrblck: <p><code>conda list | grep torch</code> shows the PyTorch installation in your custom environment?<br>
If you start a REPL session using <code>python</code> in your terminal, can you <code>import torch</code> then?<br>
Are there other PyTorch-related files in your working directory, e.g. the source code from another installation?</p>

amyxlu: <p>I just tried removing this package and reinstalling it into my conda environment, which did not resolve this error.  <code>conda list | grep torch</code> gives:</p>
<pre><code class="lang-auto">(myenv) user@host ~$ conda list | grep torch
pytorch                   1.0.1           py3.6_cuda9.0.176_cudnn7.4.2_2    pytorch
torchvision               0.1.8                    py36_0 
</code></pre>
<p>Based on another suggestion on this forum, I also tried going into the directory on my <code>$PYTHONPATH</code> and doing <code>ls | grep torch</code>, which didn’t return anything. I’m not sure if this would be related?</p>
<p>I’m using Ubuntu 16.04.6.</p>

ptrblck: <p>Could you try to activate your conda environment, uninstall all possible PyTorch packages using</p>
<pre><code class="lang-python">pip uninstall torch
pip uninstall torch
conda uninstall pytorch
</code></pre>
<p>and then try to reinstall it?<br>
I’m not sure what might go wrong, if you just open a REPL session and try to import torch after installing it.<br>
Also, make sure your working directory (from which you call <code>python</code> in the terminal) is not in any PyTorch source folder.</p>

Suraj: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>,</p>
<p>I did all that you advised. uninstalled torch completely and installed with either the conda or pip but no luck with either of them.</p>
<p>Although the problem in my case is importing torch in a jupyter notebook. No error while running any script from the terminal but error shows up in importing torch in a jupyter notebook. I also made new environment but still the error persists. Tried on different python versions as well. Please help!</p>
<p>Using Ubuntu and Anaconda.<br>
When <code>conda  list | grep torch</code> gave</p>
<pre><code class="lang-auto">torch                     1.6.0                    pypi_0    pypi
torchvision               0.7.0                    pypi_0    pypi
</code></pre>
<p>when installed with pip and</p>
<pre><code class="lang-auto">pytorch                   1.0.0           py3.7_cuda9.0.176_cudnn7.4.1_1    pytorch
torchvision               0.2.2                      py_3    pytorch
</code></pre>
<p>when installed with conda using the command on official site. Not working in any case!</p>
<p>Thanks!</p>

ptrblck: <aside class="quote no-group" data-username="Suraj" data-post="9" data-topic="41168">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/suraj/48/28176_2.png" class="avatar"> Suraj:</div>
<blockquote>
<p>Although the problem in my case is importing torch in a jupyter notebook. No error while running any script from the terminal but error shows up in importing torch in a jupyter notebook.</p>
</blockquote>
</aside>
<p>Most likely your Jupyter notebook is using a different Python kernel than the one used in the terminal.<br>
You should be able to select the right Python kernel in the notebook directly.</p>

Suraj: <p>Thankfully one of the conda env was running fine all this time but all others were “glitched” (atleast what I thought) somehow.</p>
<p>I just tried importing torch in all the “gliched” env and both the pip and conda installations are working now. <strong>I don’t even know what fixed it because I didn’t change anything from the last failed attempt</strong>. I just checked version from python shell in command line using platform package and checked the same in jupyter notebook as per your advice. Thank you.</p>