Pytorch Walkthrough on macOS

Env

macOS 10.15 | python 3.8.2 | torch 1.4.0 | torchvision 0.5.0

Install via pip

macOS binary 安装不支持 CUDA,如果需要 CUDA 请翻阅官网 install via source 安装指引。

1
2
pip3 install torch
pip3 install torchvision

安装成功后发现报错:

1
2
3
4
5
6
7
8
>>> import torch
toTraceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/__init__.py", line 97, in <module>
    from torch._C import *
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/_C.cpython-38-darwin.so, 9): Library not loaded: @rpath/libc++.1.dylib
  Referenced from: /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/_C.cpython-38-darwin.so
  Reason: image not found

libc++.1.dylib/usr/lib 下,使用 install_name_tool 解决:

1
install_name_tool -add_rpath /usr/lib /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/torch/_C.cpython-38-darwin.so

Comments