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

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)

(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 <module>
    from stanza.pipeline.core import DownloadMethod, Pipeline
  File "/Users/yiminglu/Desktop/stanza-train-master/stanza-train/stanza/stanza/pipeline/core.py", line 9, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'

Can you share how you installed pytorch? Conda, pip, or from source? If you’re using an environment, did you activate that environment?

Hey thanks so much for replying! I have been using pip and conda. These are the commands I copied and pasted from the internet.

conda:

  1. Create a conda environment with conda create -n my-torch python=3.7 -y
  2. Activate the new environment with conda activate my-torch
  3. Inside the new environment, install PyTorch and related packages with:

conda install python=3.6 pytorch torchvision matplotlib pandas -c pytorch

pip
pip3 install torch torchvision torchaudio

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.

Thanks!

The python versions in the conda section used the updated versions

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.

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?

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

Hey I got this:

(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

I have already installed in my-torch and I installed it again by this:

conda install pytorch torchvision torchaudio -c pytorch

but when I run it:

python3 -m stanza.utils.datasets.prepare_mwt_treebank UD_English-TEST

I still got the same result

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 <module>
    from stanza.pipeline.core import DownloadMethod, Pipeline
  File "/Users/yiminglu/Desktop/stanza-train-master/stanza-train/stanza/stanza/pipeline/core.py", line 9, in <module>
    import torch
ModuleNotFoundError: No module named 'torch'

Hi I downloaded virtualenv and created an environment with it. I did the same thing and it worked. Thanks so much for helping again!