'Library not loaded: @rpath/libcudart.7.5.dyli

2019-01-07 20:02发布

问题:

I'm using OS X El Capitan (10.11.4).

I just downloaded TensorFlow using the pip install instructions here.

Everything went pretty smoothly, though I did get a few warning messages like:

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.

and

You are using pip version 6.0.8, however version 8.1.2 is available. Even though I just installed pip.

Then, when I tested TensorFlow in Python, I got the error:

>>> 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

Now, when I try to do pip uninstall tensorflow-0.10.0rc0 it tells me that it's not installed.

The closest thing I've found to resembling this problem is this issue in the TensorFlow GitHub docs (which I have not tried).

How can I uninstall whatever it did install and get TensorFlow up and running correctly?

回答1:

This error message is displayed if you install the GPU-enabled Mac OS version of TensorFlow (available from release 0.10 onwards) on a machine that does not have CUDA installed.

To fix the error, install the CPU version for Python 2.7 or 3.x, as follows:

# 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

See tensorflow versions: https://www.tensorflow.org/versions/



回答2:

To add to @mrry's answer, if you already have CUDA installed but you still get the error, it could be because the CUDA libraries are not on your path. Add the following to your ~/.bashrc or ~/.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"

Uncomment either of the CUDA_HOMEs or edit it so that it contains your CUDA install. If you do not know where it is installed, try:

find / -name "*libcudart*"


回答3:

Certainly installing CUDA is essential, as is ensuring that all the paths are correct. I'm running:

  • TensorFlow 0.12r0
  • OSX 10.12.1
  • python 2.7 from brew
  • virtualenv to separate my python environments
  • CUDA 8.0.55
  • cudnn-8.0-osx-x64-v5.1

On my system I have also had further issues where it appears that the problem originates from the dynamic libraries internally referencing relative paths.

To discover the @rpath being referenced from _pywrap_tensorflow.so the following code is run:

otool -l /Users/norman_h/.virtualenvs/env_name/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so

which returned, amongst other things, the following:

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)

It can be seen that the dynamic library is attempting to find the CUDA libraries within my virtual environment where I installed TensorFlow with pip. It's not looking within my systems environment paths.

A hack around of a solution is to dynamically link the CUDA libraries from their /usr/local/cuda/lib location into the site-packages where pip installed TensorFlow inside my virtual environment.

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 .

Will need to re-link when pip upgrades TensorFlow from within the virtual environment.

I think this all goes back to the original compilation of TensorFlow that is done for the pip install and I have no idea how to submit a fix, or even if I am correct. Perhaps the original compilation of Tensorflow needs to be more dynamic and not static.

Best of luck!