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

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

问题描述

您可能想先跳到问题的结尾,我在评论/答案中遵循了一些建议,当前的错误与原始错误不同(似乎与numpy 可能).

此错误 ModuleNotFoundError: No module named 'torch' 出现在大量线程中,我一整天都在尝试解决方案.我将使用线程中建议的解决方案一一完成我的故障排除步骤.

系统信息:视窗 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

好的:

<块引用>

使用以下方法激活环境:

source activate env_pytorch

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

<块引用>

现在使用 pip 安装 PyTorch:

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

嗯......好吧,它着火了,所以下面......

<块引用>

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

导入火炬进口火炬视觉

...没有做任何新的事情,和以前一样的错误.

好吧,转到 PyTorch GitHub 上的下一个线程:

<块引用>

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

哦,好吧,我已经这样做了.没有布埃诺.

另一个线程:

好的,这是很多绿色,让我们现在试试......

好吧,我们无法赢得所有人,所以让我们进入下一个主题:

哦,另一个解决方案着火了..

<块引用>

我在 Windows 10 上遇到了类似的问题.最后我只能通过 Miniconda 安装手电筒.

好吧,让我们试试吧!

好吧,很酷,关键时刻:

太棒了!你只是通读了 25 分钟,我重新制作了我解决这个问题的所有尝试,它甚至不包括我花在一个兔子洞里试图使用 Jupyter 的时间,它同样失败了.我想是时候把问题发到 StackOverflow 上了!

编辑 1:

一个答案指出我的一个日志是一个错误 python 3.8 与 pytorch 不兼容,很好,我会解决这个问题.卸载 3.8 并安装 3.7 后:

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

编辑 2:

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

解决方案

Pytorch 需要 3.5 <= 蟒蛇 <3.8.设置环境:

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

您还应该确保从激活的 conda 环境中启动已安装的 Python 解释器(YourAnacondaInstallDirectoryenvspytorchpython.exe)!后者很重要,因为 conda 将导出某些环境变量(查看 this 对于由缺少环境变量引起的相关问题).

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 (YourAnacondaInstallDirectoryenvspytorchpython.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天全站免登陆