Python 没有名为“Torch”的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54843067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 20:31:37  来源:igfitidea点击:

No module named "Torch"

pythonpippytorchconda

提问by RedCrayon

Hello I installed pytorch via

您好,我通过以下方式安装了 pytorch

conda install pytorch-cpu torchvision-cpu -c pytorch

conda install pytorch-cpu torchvision-cpu -c pytorch

and I also tried

我也试过

pip3 install https://download.pytorch.org/whl/cpu/torch-1.0.1-cp36-cp36m-win_amd64.whl

pip3 install torchvision

Both installed successfully!

两个都安装成功!

But, it only works in jupiter notebook. Whenever I try to execute a script from the console, I get the error message: No module named "torch"

但是,它只适用于木星笔记本。每当我尝试从控制台执行脚本时,都会收到错误消息:没有名为“torch”的模块

How can I fix this?

我怎样才能解决这个问题?

回答by srilekha palepu - Intel

Try to install PyTorch using pip:

尝试使用 pip 安装 PyTorch:

First create a Conda environment using:

首先使用以下命令创建 Conda 环境:

conda create -n env_pytorch python=3.6

Activate the environment using:

使用以下方法激活环境:

conda activate env_pytorch

Now install PyTorch using pip:

现在使用 pip 安装 PyTorch:

pip install torchvision 

Note: This will install both torch and torchvision.

注意:这将安装 torch 和 torchvision。

Now go to Python shell and import using the command:

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

import torch
import torchvision

回答by prosti

You need to add this at the very top of your program

您需要在程序的最顶部添加它

import torch

If this is not a problem execute this program on both Jupiter and command line and pretty much you will understand if you have a mismatch.

如果这不是问题,请在 Jupiter 和命令行上执行此程序,如果不匹配,您就会明白。

import sys
print(sys.executable)

回答by Wigcat Lion

I installed on my macos by the official command:

我通过官方命令安装在我的 macos 上:

conda install pytorch torchvision -c pytorch

but when I follow the official verificationI get the same problem like yours.

但是当我按照官方验证进行操作时,我遇到了与您相同的问题。

Then I create a conda virtual environment:

然后我创建了一个 conda 虚拟环境:

conda create --name learnpytorch python=3.5

and install pytorch inside the environment:

并在环境中安装 pytorch:

conda install pytorch torchvision -c pytorch

run the verification, it works.

运行验证,它有效。

Hope these could help you.

希望这些能帮到你。

回答by ohlr

Welcome to SO,

欢迎来到 SO,

please create a seperate conda environment

请创建一个单独的conda 环境

activate this environment conda activate myenvand than install pytorch in it.

激活此环境conda activate myenv,然后在其中安装 pytorch。

Besides you can check which python you are currently using by which python

此外,您可以通过以下方式检查您当前使用的python which python

回答by JJTT

Usually if the torch/tensorflow has been successfully installed, you still cannot import those libraries, the reason is that the python environment you try to import is not the python environment you installed.

通常如果torch/tensorflow已经成功安装,你仍然无法导入那些库,原因是你尝试导入的python环境不是你安装的python环境。

For example, if you have installed the torch/tensorflow using python='/usr/bin/python', then you cannot import them to python='/home/usrname/.../bin/python'.

例如,如果您使用 安装了 Torch/tensorflow python='/usr/bin/python',则无法将它们导入python='/home/usrname/.../bin/python'.

The solution is simple thus, just change the python and import again.

解决方案很简单,只需更改python并再次导入即可。

回答by Peter Chen

I am struggling the above answer and it does not work in my case.
That is because I install the porch under window COMMAND PROMPT.

我正在为上述答案而苦苦挣扎,但在我的情况下它不起作用。
那是因为我在窗口命令提示符下安装了门廊。

  1. Installed under the Anaconda environment command prompt. To do that, type "anaconda" and select the ANACONDA COMMAND PROMPT (this is very important for me to fix my mistake)
  2. Do the same command from: https://pytorch.org/get-started/locally/
  1. 安装在 Anaconda 环境命令提示符下。为此,请输入“anaconda”并选择 ANACONDA COMMAND PROMPT(这对我修复错误非常重要)
  2. 执行相同的命令:https: //pytorch.org/get-started/locally/

回答by prometeu

Create the environment:

创建环境:

conda create -n env_pytorch python=3.6

Install your modules. For example:

安装你的模块。例如:

conda install pytorch torchvision cudatoolkit=10.2 -c pytorch

Next time you want to use pytorch:

下次你想使用 pytorch 时:

conda activate env_pytorch

and when finished:

完成后:

conda deactivate