ModuleNotFoundError: No module named 'torch' even if I installed it in multiple environment

yiming_lu: <p>Hi. I have been installing pytorch multiple times. I took into consideration the python environment I was using. After all these, the code that I run still says ModuleNotFoundError: No module named ‘torch’. Could anyone help me in these. I copy the error message below. (I was trying to use stanza to evaluate a model, sorry just a beginner)</p>
<pre><code class="lang-auto">(my-torch) Toufig-Lu:stanza yiminglu$ python3 -m stanza.utils.datasets.prepare_mwt_treebank UD_English-TEST
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "/Users/yiminglu/Desktop/stanza-train-master/stanza-train/stanza/stanza/__init__.py", line 1, in &lt;module&gt;
    from stanza.pipeline.core import DownloadMethod, Pipeline
  File "/Users/yiminglu/Desktop/stanza-train-master/stanza-train/stanza/stanza/pipeline/core.py", line 9, in &lt;module&gt;
    import torch
ModuleNotFoundError: No module named 'torch'
</code></pre>

AlphaBetaGamma96: <p>Can you share how you installed pytorch? Conda, pip, or from source? If you’re using an environment, did you activate that environment?</p>

yiming_lu: <p>Hey thanks so much for replying! I have been using pip and conda. These are the commands I copied and pasted from the internet.</p>
<p>conda:</p>
<ol>
<li>Create a conda environment with <code>conda create -n my-torch python=3.7 -y</code>
</li>
<li>Activate the new environment with <code>conda activate my-torch</code>
</li>
<li>Inside the new environment, install <em>PyTorch</em> and related packages with:</li>
</ol>
<p><code>conda install python=3.6 pytorch torchvision matplotlib pandas -c pytorch</code></p>
<p>pip<br>
pip3 install torch torchvision torchaudio</p>
<p>I did these multiple times. I created another environment in anaconda, and I did it directly in anaconda, I used pip. At the end, the response is all the same.</p>
<p>Thanks!</p>

yiming_lu: <p>The python versions in the conda section used the updated versions</p>

AlphaBetaGamma96: <p>Ok, so let’s use the conda environment for the meanwhile. Are you using an integrated developer environment (IDE) like PyCharm, VScode, etc? If so, you’ll need to select the environment within the IDE as well (instead of just the terminal) and select the correct python interpreter, because it’ll default to your base environment that doesn’t have pytorch installed.</p>

yiming_lu: <p>hey I am actually running codes written by others. I am running them in bash terminal. Should I check which IDE bash uses in default?</p>

AlphaBetaGamma96: <p>You can check what environment you’re using (in conda) via using <code>conda env list</code> within the terminal, you’ll see a <code>*</code> next to the environment you’re using. Could you check that?</p>

yiming_lu: <p>Hey I got this:</p>
<pre><code class="lang-auto">(my-torch) Toufig-Lu:stanza yiminglu$ conda env list 
# conda environments:
#
base                     /Users/yiminglu/opt/anaconda3
env_pytorch              /Users/yiminglu/opt/anaconda3/envs/env_pytorch
learnpytorch             /Users/yiminglu/opt/anaconda3/envs/learnpytorch
my-torch              *  /Users/yiminglu/opt/anaconda3/envs/my-torch
my_stanza                /Users/yiminglu/opt/anaconda3/envs/my_stanza
</code></pre>
<p>I have already installed in my-torch and I installed it again by this:</p>
<pre><code class="lang-auto">conda install pytorch torchvision torchaudio -c pytorch
</code></pre>
<p>but when I run it:</p>
<pre><code class="lang-auto">python3 -m stanza.utils.datasets.prepare_mwt_treebank UD_English-TEST
</code></pre>
<p>I still got the same result</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 187, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/runpy.py", line 110, in _get_module_details
    __import__(pkg_name)
  File "/Users/yiminglu/Desktop/stanza-train-master/stanza-train/stanza/stanza/__init__.py", line 1, in &lt;module&gt;
    from stanza.pipeline.core import DownloadMethod, Pipeline
  File "/Users/yiminglu/Desktop/stanza-train-master/stanza-train/stanza/stanza/pipeline/core.py", line 9, in &lt;module&gt;
    import torch
ModuleNotFoundError: No module named 'torch'
</code></pre>

yiming_lu: <p>Hi I downloaded virtualenv and created an environment with it. I did the same thing and it worked. Thanks so much for helping again!</p>

Faegheh_Yeganli: <p>Hi,<br>
I Have the same issue, how did you do the last step which solved your problem?</p>

yiming_lu: <p>Hi I solved the problem following this: <a href="https://gist.github.com/Ravi2712/47f070a6578153d3caee92bb67134963" class="inline-onebox" rel="noopener nofollow ugc">Install Pytorch with virtualenv · GitHub</a>.<br>
basically install a virtual environment and install pytorch there. I hope it will work for you. But still, I don`t know what goes wrong with the normal environment. It worked fine and one day it no longer worked.</p>

Faegheh_Yeganli: <p>Thank you very much,<br>
What about to create conda env. and I still have the same problem.<br>
conda create --name lab python=3.8 -y<br>
conda activate lab<br>
I even install the torch like:<br>
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia<br>
and after that when I mim install mmcv-full it shows me the error of:</p>
<p>ModuleNotFoundError: No module named ‘torch’</p>

yiming_lu: <p>Hi I don`t know too much. I am new to this, so I might not be answering your question. I did:</p>
<pre><code class="lang-auto">$ python3 -m pip install --user virtualenv #Install virtualenv if not installed in your system
$ python3 -m virtualenv env #Create virtualenv for your project
$ source env/bin/activate #Activate virtualenv for linux/MacOS
$ env\Scripts\activate
</code></pre>
<p>And then I pip installed pytorch.</p>
<pre><code class="lang-auto">pip3 install torch torchvision torchaudio
</code></pre>
<p>Things work after that. I use anaconda and it works fine. No more  <code>No module named 'torch'</code></p>

folterj: <p>I had the same issue.<br>
Using pip is a work-around, not a solution. It seems adding <code>torchvision</code> is essential.</p>
<p>One-liner:<br>
<code>conda install pytorch torchvision pytorch-cuda -c pytorch -c nvidia</code></p>
<p>Example env.yaml:</p>
<pre><code class="lang-auto">name: env_name
channels:
  - pytorch
  - nvidia
  - conda-forge
  - defaults
dependencies:
  - python=3.10
  - pytorch-cuda
  - torchvision
  - numpy
</code></pre>

cuichengyuan1999: <p>thx, This post really helps me a lot, I have the same issue of having installed pytorch by pip, and using command pip show I can see torch is correctly configured, but I can’t use torch in my code, I still don’t make out why</p>