ModuleNotFoundError: No module named 'torch' in spyder

(John Cwok ) #1

Hi !

I installed pytorch recently, following the command on the pytorch website (i am using anaconda).
When I use the anaconda prompt to open a python intepreter the import torch line works perfectly, but when I try it in spyder I get the error :
import torch ModuleNotFoundError: No module named 'torch'
I tried creating a conda environment, activating it and importing pytorch inside of it but it did’t change anything.
I am running on window 10 without cuda.

Do you guys know what I can do ? I know this issue has already been posted but none of the proposed solution worked for me which is why i am posting.

(Juan F Montesinos) #2

Hi, have you verified that spyder points to proper python iterpreter? Realize you cannot install several spyder versions (namely, one for python2 and another for python3).

That’s probably the issue you are facing (somehow)

(tymokvo) #3

You can follow @JuanFMontesinos advice with:

import sys

And ensure the executable that is pointed to is the one with torch installed.

(John Cwok ) #4

This returns C:\Users\cnelias\Anaconda3\pythonw.exe which i think should be correct ? When I run conda info --envs
I only have the following :
base * C:\Users\cnelias\.julia\conda\3 C:\Users\cnelias\Anaconda3. Or am I doing this wrong ?

(tymokvo) #5

Ugh, it’s hard to help debug an environment setup. It also looks like you have installed julia which I have had totally ruin a Conda setup on my windows machine before. To be honest, I have entirely stopped using environment managers like Conda as I think they cause more problems than they solve.

Could you try making a native python virtual environment to test? That will isolate config problems to Spyder or Conda.

I use something like:
(from a command prompt)

>python -m venv pytorch # venv is the python native virtual environment manager
(pytorch) >pip install [enter the correct pip package here]

And then launch python from that environment and try some torchin’.

The venv command will create a directory with the name that you give it which contains the whole virtual environment. So I keep all of mine in a directory called PythonEnvironments.