我正在使用OS X El Capitan(10.11.4)。
我刚刚使用pip安装说明here下载了TensorFlow。
一切都很顺利,但我收到了一些警告信息,如:
The directory '/Users/myusername/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want the -H flag.
和
You are using pip version 6.0.8, however version 8.1.2 is available.
即使我刚刚安装了pip。
然后,当我在Python中测试TensorFlow时,我收到了错误:
>>> import tensorflow as tf
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/__init__.py", line 23, in <module>
from tensorflow.python import *
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/__init__.py", line 48, in <module>
from tensorflow.python import pywrap_tensorflow
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
_pywrap_tensorflow = swig_import_helper()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
_mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/imp.py", line 243, in load_module
return load_dynamic(name, filename, file)
ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/_pywrap_tensorflow.so, 10): Library not loaded: @rpath/libcudart.7.5.dylib
Referenced from: /Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/tensorflow/python/_pywrap_tensorflow.so
Reason: image not found
现在,当我尝试pip uninstall tensorflow-0.10.0rc0
时,它告诉我它没有安装。
我发现最接近这个问题的是TensorFlow GitHub文档中的this issue(我还没试过)。
如何卸载它安装的任何内容并使TensorFlow正常运行?
答案 0 :(得分:35)
如果您在未安装CUDA的计算机上安装支持GPU的Mac OS版TensorFlow(从版本0.10开始提供),则会显示此错误消息。
要修复错误,请安装Python 2.7或3.x的CPU版本,如下所示:
# Mac OS X, CPU only, Python 2.7:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py2-none-any.whl
$ sudo pip install --upgrade $TF_BINARY_URL
# Mac OS X, CPU only, Python 3.4 or 3.5:
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.12.0-py3-none-any.whl
$ sudo pip3 install --upgrade $TF_BINARY_URL
请参阅tensorflow版本:https://www.tensorflow.org/versions/
答案 1 :(得分:3)
要添加到@mrry's answer,如果您已经安装了CUDA但仍然收到错误,那可能是因为CUDA库不在您的路径上。将以下内容添加到〜/ .bashrc或〜/ .zshrc:
# export CUDA_HOME=/Developer/NVIDIA/CUDA-7.5 ## This is the default location on macOS
export CUDA_HOME=/usr/local/cuda
export DYLD_LIBRARY_PATH="$CUDA_HOME/lib:$DYLD_LIBRARY_PATH"
export PATH="$CUDA_HOME/bin:$PATH"
取消注释CUDA_HOME
之一或编辑它以使其包含您的CUDA安装。如果您不知道它的安装位置,请尝试:
find / -name "*libcudart*"
答案 2 :(得分:2)
当然,安装CUDA至关重要,因为确保所有路径都是正确的。我正在跑步:
在我的系统上,我还遇到了进一步的问题,问题似乎来自内部引用相对路径的动态库。
要发现从@rpath
引用的_pywrap_tensorflow.so
,请运行以下代码:
otool -l /Users/norman_h/.virtualenvs/env_name/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so
除其他外,还返回以下内容:
Load command 15
cmd LC_RPATH
cmdsize 128
path $ORIGIN/../../_solib_darwin/_U@local_Uconfig_Ucuda_S_Scuda_Ccudart___Uexternal_Slocal_Uconfig_Ucuda_Scuda_Slib (offset 12)
Load command 16
cmd LC_RPATH
cmdsize 48
path ../local_config_cuda/cuda/lib (offset 12)
Load command 17
cmd LC_RPATH
cmdsize 56
path ../local_config_cuda/cuda/extras/CUPTI/lib (offset 12)
可以看出,动态库试图在我用我的pip安装TensorFlow的虚拟环境中找到CUDA库。它没有在我的系统环境路径中查找。
解决方案的一个方法是将CUDA库从/usr/local/cuda/lib
位置动态链接到我的虚拟环境中pip安装了TensorFlow的站点包。
mkdir /Users/norman_h/.virtualenvs/env_name/lib/python2.7/site-packages/tensorflow/local_config_cuda
cd /Users/norman_h/.virtualenvs/env_name/lib/python2.7/site-packages/tensorflow/local_config_cuda
ln -s /usr/local/cuda .
当pip从虚拟环境中升级TensorFlow时,需要重新链接。
我认为这一切都可以追溯到为Pip安装完成的TensorFlow的原始编译,我不知道如何提交修复,或者即使我是正确的。也许Tensorflow的原始编辑需要更加动态而不是静态。
祝你好运!
答案 3 :(得分:0)
当我尝试导入pyTorch时,此问题在我的macOS上弹出。我在japanese site中找到了解决方案,它无法做出正面和反面,而只是给出了brew install libomp
的解决方案。干杯!很抱歉张贴到旧主题,但我认为有必要。