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

I’ve double checked to ensure that the conda environment is activated. I redownloaded PyTorch using conda install -c pytorch pytorch. When I do conda list I can see pytorch 1.0.1 py3.6_cuda10.0.130_cudnn7.4.2_2 pytorch listed.

Not too sure what else I can try to figure out why I’m getting this error. Any help would be appreciated!

Do you get this error in the terminal or are you using any IDE?

Nope, not using an IDE. I’m running it directly from the terminal using python script.py.

Did you create a specific conda environment before installing PyTorch?

Yes, I did. I’d checked to make sure that the custom conda environment into which torch was installed was activated.

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

I just tried removing this package and reinstalling it into my conda environment, which did not resolve this error. conda list | grep torch gives:

(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 

Based on another suggestion on this forum, I also tried going into the directory on my $PYTHONPATH and doing ls | grep torch, which didn’t return anything. I’m not sure if this would be related?

I’m using Ubuntu 16.04.6.

Could you try to activate your conda environment, uninstall all possible PyTorch packages using

pip uninstall torch
pip uninstall torch
conda uninstall pytorch

and then try to reinstall it?
I’m not sure what might go wrong, if you just open a REPL session and try to import torch after installing it.
Also, make sure your working directory (from which you call python in the terminal) is not in any PyTorch source folder.

Hi @ptrblck,

I did all that you advised. uninstalled torch completely and installed with either the conda or pip but no luck with either of them.

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!

Using Ubuntu and Anaconda.
When conda list | grep torch gave

torch                     1.6.0                    pypi_0    pypi
torchvision               0.7.0                    pypi_0    pypi

when installed with pip and

pytorch                   1.0.0           py3.7_cuda9.0.176_cudnn7.4.1_1    pytorch
torchvision               0.2.2                      py_3    pytorch

when installed with conda using the command on official site. Not working in any case!

Thanks!

Most likely your Jupyter notebook is using a different Python kernel than the one used in the terminal.
You should be able to select the right Python kernel in the notebook directly.

1 Like

Thankfully one of the conda env was running fine all this time but all others were “glitched” (atleast what I thought) somehow.

I just tried importing torch in all the “gliched” env and both the pip and conda installations are working now. I don’t even know what fixed it because I didn’t change anything from the last failed attempt. 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.