Can't import torch on Pytorch 0.4.1

The torch installation script on pytorch.org has been changed and it affects Windows users who do not have a gpu. Previously if you do not have a gpu/or use cuda, the generated output would just be designated “pytorch” which enables “torch” in import torch. Now the script is generating “pytorch-cpu” which does not enable “torch” when import torch is a line in a script requiring torch. I get the “module not found” message when importing torch on ipynb files but not when importing and running torch-related scripts on Spyder. I an currently using a Windows 10 computer

Could you check the kernel in your Jupyter notebook?
As I understand the issue, PyTorch can be properly imported in Spyder, while Jupyter notebooks don’t seem to find the module, right?

I am pretty much biased towards using PyCharm :raised_hands:
You can download the free community edition or if you are a student and have a .edu account you can download the professional version.

After linking the Python Interpreter to your correct Python, you would not face this problem hopefully.

Here’s a quick walkthrough on how to configure your interpreter:
https://www.jetbrains.com/help/pycharm/configuring-python-interpreter.html

Hi Peter

yes that is exactly the case. I used to use torch in Jupyter notebook until PyTorch 0.4.1 came along

Not sure how to test the kernel in Jupyter Notebook, Peter. Newbie at that sort of thing here

Peter, its a Python 3.6 kernel if thats what you mean ?

Yes, that’s what I meant. Do you have any other kernels in the “change kernel” drop-down menu?

No Peter, the “change kernel” drop- down list just says Python 3

Alright, then could you try to create a new conda environment and check if that’s working?
To work with environments, have a look at this doc.
Basically, you could try the following in your terminal:

conda create -n pytorch python=3.7 anaconda
conda install pytorch torchvision -c pytorch
conda activate pytorch
jupyter notebook

Note that you might want to change the Python version or the pytorch install command if you are using another version.

We already had some issues with notebooks not being able to find certain packages and the reason for this is usually a misconfigured environment.

Thanks Peter, will give it a shot

Hi Peter, I gave your instruction a try with the end result when I tried to import torch in jupyter note book: ModuleNotFoundError Traceback (most recent call last)
in
----> 1 import torch

ModuleNotFoundError: No module named ‘torch’

Testing loading pytorch in a) the base python environment as your note suggested as well as b) installing pytorch in the newly created pytorch env with the same result.
I used the command conda install pytorch-cpu -c pytorch to install pytorch and pip3 install torchvision both in the base environment as well as the pytorch environment with the same result. These commands were listed in the current PyTorch Resources.

In the older PyTorch releases when PyTorch was installed, scripts looking for “torch” found that command but in this later version (0.4.1) thats not the case. Hopefully one of the Pytorch developers will review this . As mentioned earlier, I can use “torch” in spyder but not in Jupyter Notrbook.

Thanks for your assistance, Peter

Hi Peter
I figured this issue out finally. The problem that I was facing with PyTorch was the same as in using Tensorflow. I checked the kernel specs being used by Spyder and Jupyter Notebook (import sys then sys.executable) and they differed. So I ran “python -m ipykernel install --user” in my DeepLearning env and that fixed it. Thought that you should know this

Clive

That’s good to hear!
Thanks for the update. :slight_smile: