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
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):
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:
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:
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:
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:
Now install PyTorch using pip:
pip install torchvision --user ( this will install both torch and torchvision)
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:
@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:
Ok that's a lot of green, let's try now...
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:
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!
Alright, cool, moment of truth:
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:
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:
To install PyTorch, you have to run the installation command of PyTorch on your command prompt. This command is available on https://pytorch.org/. Select language and cuda version as per your requirement. Now, run python -version, and Conda -version command to check Conda and python packages are installed or not.
You can use torch. __version__ to check the version of PyTorch. If you have not imported PyTorch, use import torch first. If you used pip to install PyTorch, run pip3 show torch to show all the information of the installation, which also includes the version of PyTorch.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With