Mac在jupyter notebook上import torch出错:ModuleNotFoundError: No module named 'torch'

在jupyter notebook上

import torch

的时候出错了:

ModuleNotFoundError: No module named 'torch'

记录一下解决的过程:

检查问题

在终端输入

pip install pytorch

出现一大堆错误:

Collecting pytorch
  Downloading pytorch-1.0.2.tar.gz (689 bytes)
Building wheels for collected packages: pytorch
  Building wheel for pytorch (setup.py) ... error
  ERROR: Command errored out with exit status 1:
  ...
  ----------------------------------------
  ERROR: Failed building wheel for pytorch
  Running setup.py clean for pytorch
Failed to build pytorch
Installing collected packages: pytorch
    Running setup.py install for pytorch ... error
    ERROR: Command errored out with exit status 1:
    ...
    Exception: You tried to install "pytorch". The package named for PyTorch is "torch"
    ----------------------------------------
ERROR: Command errored out with exit status 1:

网上查找

找了几篇文章:
文章1
文章2
文章3
最后一篇提到从PyTorch官网下载安装。
根据我自己的情况,选择:

PyTorch Build Stable (1.4)
Your OS Mac
Package Conda
Language Python
CUDA None
Run this Command:
conda install pytorch torchvision -c pytorch
#MacOS Binaries dont support CUDA, install from source if CUDA is needed

问题解决

试运行代码:

conda install pytorch torchvision -c pytorch

最后显示:

Proceed ([y]/n)?

输入y即可。

Downloading and Extracting Packages
pytorch-1.4.0        | 34.5 MB   | ##################################### | 100% 
ninja-1.9.0          | 90 KB     | ##################################### | 100% 
torchvision-0.5.0    | 5.8 MB    | ##################################### | 100% 
Preparing transaction: done
Verifying transaction: done
Executing transaction: done

最后验证安装成功:
输入:

python3

进入python3命令行界面,输入:

from __future__ import print_function
import torch
x = torch.rand(5, 3)
print(x)

输出:

tensor([[0.9428, 0.3059, 0.7485],
        [0.5177, 0.7503, 0.0502],
        [0.0629, 0.6761, 0.4595],
        [0.1487, 0.0652, 0.3746],
        [0.7609, 0.9052, 0.5963]])

说明安装成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值