使用mac安装Python的torch包出现ImportError错误

使用mac安装torch、torchvision出现ImportError错误

使用mac安装Pytorch出现错误,安装torch成功之后,import torch出现错误 from torch._C import * 、Reason: image not found

Traceback (most recent call last):

  File "", line 1, in 

  File "/Users/jiangchao10/Library/Python/2.7/lib/python/site-packages/torch/__init__.py", line 81, in 

    from torch._C import *

ImportError: dlopen(/Users/jiangchao10/Library/Python/2.7/lib/python/site-packages/torch/_C.so, 9): Library not loaded: @rpath/libc++.1.dylib

  Referenced from: /Users/jiangchao10/Library/Python/2.7/lib/python/site-packages/torch/_C.so

  Reason: image not found

对于该种情况,转载文章https://blog.csdn.net/qq_41024144/article/details/105742035

在命令行输入:(其中两条指令的路径均为出现错误的Referenced from的路径)

第一步:otool -L /Users/jiangchao10/Library/Python/2.7/lib/python/site-packages/torch/_C.so

正常输出为:

/Users/jiangchao10/Library/Python/2.7/lib/python/site-packages/torch/_C.so:

@rpath/libshm.dylib (compatibility version 0.0.0, current version 0.0.0)

@rpath/libtorch_python.dylib (compatibility version 0.0.0, current version 0.0.0)

@rpath/libc++.1.dylib (compatibility version 1.0.0, current version 1.0.0)

/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1238.60.2)

第二步:install_name_tool -change @rpath/libc++.1.dylib /usr/lib/libc++.1.dylib /Users/jiangchao10/Library/Python/2.7/lib/python/site-packages/torch/_C.so

无任何输出

再次导入即可正常import torch

倘若第一步出现如下错误

xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools), missing xcrun at: /Library/Developer/CommandLineTools/usr/bin/xcrun

对于该种情况,转载文章https://www.cnblogs.com/aresxin/p/7903242.html

对于xcrun错误,命令行输入xcode-select --install指令即可

再按照指令指令执行即可

 

你可能感兴趣的:(python)