9

Edit: You might want to skip to the end of the question first, I've followed some advice in comments / answers and the current error is different from the original (appears to be related to numpy possibly).

This error ModuleNotFoundError: No module named 'torch' shows up in tons of threads, I've been trying solutions all day. I'll go through my troubleshooting steps one by one, using the solutions suggested in threads.

System info: Windows 10

enter image description here enter image description here

First thing I did was follow the instructions on Pytorch, installed Anaconda and did this using the correct settings for my machine (Note: I tried Python v3.7 before trying v3.8 in these screenshots, none of the solutions worked with that either):

enter image description here

enter image description here

As you can see, that should be good to go, according to the instructions.

So I go into the python terminal and try to import pytorch, like so:

enter image description here

ModuleNotFoundError: No module named 'torch' Great, so what now? Well I paste the error into Google and begin my 4 hour wild goose chase.

First result, stack overflow answer: No module named "Torch" Let's try the selected answer, it requires some version-related syntax so lets check my python version:

enter image description here

Alright so as directed by the answer:

Try to install PyTorch using pip:

First create a conda environment using:

conda create -n env_pytorch python=3.6

Ok:

enter image description here

Activate the environment using:

source activate env_pytorch

That doesnt work, but if we activate using the instructions given by the prompt, we can do so:

enter image description here

Now install PyTorch using pip:

pip install torchvision --user ( this will install both torch and torchvision)

enter image description here

Hmmm.. well that went up in flames, so the following...

Now go to python shell and import using the command:

import torch
import torchvision

...doesn't do anything new, same error as before.

Well, to the next thread, on PyTorch GitHub: https://github.com/pytorch/pytorch/issues/4827

They're trying to use Jupyter, so I tried this, is was another long process like the above that went up in flames, and I really dont want to need to use Jupyter anyway, so we'll skip this one.

Another Pytorch GitHub thread: https://github.com/pytorch/pytorch/issues/12004

@edtky Could you please give me the output of the following commands in CMD?

where conda.exe where pip.exe where python.exe

Sure I'll give it a shot:

enter image description here

@edtky Looks like you have two Python environments. Please try importing torch in Anaconda Prompt.

Oh well, I already did that. No bueno.

Another thread: https://discuss.pytorch.org/t/modulenotfounderror-no-module-named-torch/7309 suggests:

In that case you’ve probably forgotten to activate the environment where pytorch is installed. It can also be the library missing in your PYTHONPATH variable.

Well I did activate the environment as shown above, but I dont know anything about a PYTHONPTH variable, seems like the PyTorch setup guide wouldve mentioned if I needed to manually do that, I have no clue how to do it and you aren't explaining, so lets look for other answers.

Someone made a whole article to give us this little gym of advice: https://medium.com/@valeryyakovlev/anaconda-no-module-named-torch-ead10946de66

Another beginner error I encountered when started to use pytorch in anaconda environment import torch ModuleNotFoundError: No module named ‘torch’ the proper way to install pytorch to anaconda is following conda install -c pytorch pytorch It’s not enough to simply run “conda install pytorch” — the package won’t be found. So first activate your conda profile with “source activate {your_profile}” and then run the command conda install -c...

Ok thats new info, let's try that command again now that our env is activated:

enter image description here enter image description here

Ok that's a lot of green, let's try now...

enter image description here

Well we can't win 'em all, so lets go onto the next thread: https://forums.fast.ai/t/modulenotfounderror-no-module-named-torch-windows-10/12438/2

I had also faced the similar problem , I just installed torch and torchvision using pip and it worked …

Ok! Let's try:

enter image description here enter image description here

Oh well, another solution up in flames..

I ran into a similar issue with Windows 10. In the end I could only get torch installed with Miniconda.

Alrighty, lets try it!

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

Alright, cool, moment of truth:

enter image description here

Awesome! You just read through 25 minutes of me re-producing all my attempts to solve this problem, and it doesnt even include the hour I spend down a rabbit hole trying to use Jupyter, which failed equally as miserably. I think it's time to post the question to StackOverflow!

Edit 1:

An answer points out that one of my logs was an error python 3.8 isn't compatible with pytorch, good point I'll fix that. After unintalling 3.8 and installing 3.7:

enter image description here enter image description here enter image description here

And no luck! Remember I actually mentioned in my first paragraph that while I was trying 3.8 in these screenshots, the first time around I did all of this with 3.7

Edit 2:

I forgot to install after activating the environment in the previous edit. Once I fixed that, there's a new error:

enter image description here

J.Todd
  • 707
  • 1
  • 12
  • 34
  • "Ok that's a lot of green". But did you read the big error in the middle!? It says that your python version isn't compatible with pytorch. It requires python 3.5, 3.6, or 3.7. – jodag Nov 15 '19 at 04:12
  • @jodag before I tried 3.8, I was using 3.7, look at the first paragraph of my post :/ – J.Todd Nov 15 '19 at 11:33
  • @jodag just tried it again anyway, see my last edit. I WISH that had been all my problem was. Thatd be fantastic lol – J.Todd Nov 15 '19 at 12:00
  • Did you ever fix this? I literally have the same problem and I cannot make any progress no matter what I do. I am forced to use colab. – John Stud Oct 17 '21 at 18:22
  • Same for me, no solution yet ! – Entretoize Jan 10 '22 at 08:20

3 Answers3

4

Pytorch requires 3.5 <= python < 3.8. Setup an environment with:

conda create -n pytorch python=3.7
conda activate pytorch
conda install pytorch

You should also make sure that you launch the installed python interpreter from this environment (YourAnacondaInstallDirectory\envs\pytorch\python.exe) from the activated conda environment! The later is important because conda will export certain environment variables (have a look at this for a related issue caused by missing envionment variables).

cronoik
  • 15,434
  • 3
  • 40
  • 78
  • Just tried this, please see my edit at the bottom of my answer for the resuts – J.Todd Nov 15 '19 at 11:59
  • Edit: See new edit, I forgot to run the install command after that update, now that Ive done that, there's a new error, which i show in the most recent edit at the end of my question – J.Todd Nov 15 '19 at 16:36
  • New error after following your suggestion: `>>> import torch Traceback (most recent call last): File "", line 1, in File "C:\Users\Jonathan\Miniconda3\envs\pytorch\lib\site-packages\torch\__init__.py", line 81, in from torch._C import * ImportError: numpy.core.multiarray failed to import` I've actually already seen that numpy error before, and last time I re-installed numpy and got more errors. But this shoudl be working more or less out of the box, so something else is wrong here, Anaconda is supposed to have all the dependencies figured out for us – J.Todd Nov 15 '19 at 16:38
  • @Viziionary: Sure it does and it also has installed numpy, but can't find the libs due to PATH issue ([have a look at this for more information](https://github.com/ContinuumIO/anaconda-issues/issues/1508)). Can you please open a cmd and execute: `PATH=ANACONDAFOLDER/envs/pytorch/Lib/site-packages/numpy/.libs;%PATH%` and launch `ANACONDAFOLDER/envs/pytorch/python.exe`. Can you import numpy and torch from this cmd? – cronoik Nov 16 '19 at 03:16
  • When I was following your instructions a few days ago I accidentally did `PATH=` without `;%PATH%` at the end and figure at that point everything installed at the command line level is now useless (unless I could recall everything in path and find the locations and fix it, which I have no clue) and threw my hands up and gave up on python for a while lol – J.Todd Nov 20 '19 at 18:09
  • PyTorch is now compatible with 3.X – John Stud Oct 14 '21 at 15:45
  • @JohnStud It is still recommended to use 3.6-3.8 [link](https://pytorch.org/get-started/locally/#linux-python). – cronoik Oct 16 '21 at 16:40
1

Use This Let me Know if it will Work!

pip install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp37-cp37m-win_amd64.whl
1

If you would like to install it with plain pip rather than conda:

As you know, Pytorch 1.5.1 requires 3.5 <= python < 3.8. Use Python 3.7 or slightly earlier.

Do NOT try to install with simple pip install torch. Instead, as you say, go to https://pytorch.org/, in the colorful grid, click on pip, copy the command, open a command prompt as an administrator (right-click and select "Run as Administrator") then paste the command, which should look something like:

pip install torch===1.5.1 torchvision===0.6.1 -f https://download.pytorch.org/whl/torch_stable.html

Then, edit the command to replace pip with the full path to your version of pip, e.g.:

"C:\Program Files\Python37\Scripts\pip.exe" pip install torch===1.5.1 torchvision===0.6.1 -f https://download.pytorch.org/whl/torch_stable.html

(You don't need to edit the command as long as Python 3.7 is in your path.)

Josiah Yoder
  • 3,321
  • 4
  • 40
  • 58
  • I don't know for which versions of PyTorch the Python version limitations apply. But my previous answer implies it is true **at least** for 1.5.1 – Josiah Yoder Sep 09 '22 at 17:45