如何在Windows上修复此pytorch错误?(ModuleNotFoundError:没有名为"torch"的模块) [英] How can I fix this pytorch error on Windows? (ModuleNotFoundError: No module named 'torch')

查看:64
本文介绍了如何在Windows上修复此pytorch错误?(ModuleNotFoundError:没有名为"torch"的模块)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:您可能想先跳到问题末尾,我在注释/答案中遵循了一些建议,当前错误与原始错误有所不同(似乎与可能是numpy).

此错误 ModuleNotFoundError:没有名为"torch"的模块出现在大量线程中,我整天都在尝试解决方案.我将使用线程中建议的解决方案来一步一步地进行故障排除.

系统信息:Windows 10

我要做的第一件事是按照Pytorch上的说明进行操作,安装Anaconda并使用正确的机器设置进行操作(注意:我在这些屏幕截图中尝试了v3.8之前尝试了Python v3.7,所有解决方案都无法解决该问题)

如您所见,按照说明进行操作应该很好.

因此,我进入python终端并尝试导入pytorch,如下所示:

ModuleNotFoundError:没有名为"torch"的模块太好了,那又如何呢?好吧,我将错误粘贴到Google并开始进行4小时的野鹅追逐.

第一个结果,堆栈溢出答案:

好的,按照答案的指示:

尝试使用pip安装PyTorch:

首先使用以下命令创建一个conda环境:

  conda create -n env_pytorch python = 3.6 

好:

使用以下方法激活环境:

 源代码激活env_pytorch 

那是行不通的,但是如果我们按照提示给出的指示进行激活,我们可以这样做:

现在使用pip安装PyTorch:

  pip install torchvision --user(这将同时安装torch和torchvision) 

嗯..那在火焰中上升了,所以接下来...

现在转到python shell并使用以下命令导入:

 导入火炬进口火炬 

...没有做任何新的事情,与以前相同的错误.

在PyTorch GitHub上,转到下一个线程:

@edtky看起来您有两个Python环境.请试试在Anaconda Prompt中导入火炬.

哦,我已经做到了.没有布宜诺斯艾利斯.

另一个线程:

好吧,绿色很多,让我们现在尝试...

好吧,我们不能赢得全部,所以让我们进入下一个线程:

哦,好了,另一个解决方案.

我在Windows 10中遇到了类似的问题.最后,我只能在Miniconda上安装割炬.

好的,让我们尝试一下!

好吧,很酷,关键时刻:

太棒了!您只读了25分钟,便重新制作了解决此问题的所有尝试,甚至还没有包括我花了一个小时尝试使用Jupyter的时间,这同样惨败.我认为是时候将问题发布到StackOverflow了!

一个答案指出我的一个日志是错误python 3.8与pytorch不兼容,好一点我会解决.取消安装3.8并安装3.7之后:

没有运气!记住,我实际上在第一段中提到,当我在这些屏幕截图中尝试3.8时,我第一次使用3.7完成了所有这些操作

在上次编辑中激活环境后,我忘记安装 .一旦我解决了这个问题,就会出现一个新错误:

解决方案

Pytorch需要 3.5 < = python< 3.8 .通过以下方式设置环境:

  conda create -n pytorch python = 3.7康达激活pytorch康达安装pytorch 

您还应该确保从激活的conda环境中从此环境( YourAnacondaInstallDirectory \ envs \ pytorch \ python.exe )启动已安装的python解释器!后者很重要,因为conda会导出某些环境变量(请查看针对由于缺少环境变量而引起的相关问题).

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:

解决方案

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).

这篇关于如何在Windows上修复此pytorch错误?(ModuleNotFoundError:没有名为"torch"的模块)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆