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'
1 Like

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.

1 Like

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!

Hi,
I Have the same issue, how did you do the last step which solved your problem?

Hi I solved the problem following this: Install Pytorch with virtualenv · GitHub.
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.

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

ModuleNotFoundError: No module named ‘torch’

Hi I don`t know too much. I am new to this, so I might not be answering your question. I did:

$ 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

And then I pip installed pytorch.

pip3 install torch torchvision torchaudio

Things work after that. I use anaconda and it works fine. No more No module named 'torch'

I had the same issue.
Using pip is a work-around, not a solution. It seems adding torchvision is essential.

One-liner:
conda install pytorch torchvision pytorch-cuda -c pytorch -c nvidia

Example env.yaml:

name: env_name
channels:
  - pytorch
  - nvidia
  - conda-forge
  - defaults
dependencies:
  - python=3.10
  - pytorch-cuda
  - torchvision
  - numpy