在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]])
说明安装成功!