Cannot find dynamic library when running a Python script from Bazel
<p>I am trying to setup CUDA enabled Python &amp; TensorFlow environment on OSx 10.11.6</p>

<p>Everything went quite smoothly. First I installed following:</p>

<ul>
<li>CUDA - 7.5</li>
<li>cuDNN - 5.1</li>
</ul>

<p>I ensured that the LD_LIBRARY_PATH and CUDA_HOME are set properly by adding following into my ~/.bash_profile file:</p>

<pre><code>export CUDA_HOME=/usr/local/cuda 
export DYLD_LIBRARY_PATH="$CUDA_HOME/lib:$DYLD_LIBRARY_PATH"
export LD_LIBRARY_PATH="$CUDA_HOME/lib:$LD_LIBRARY_PATH"
export PATH="$CUDA_HOME/bin:$PATH"
</code></pre>

<p>Then I used Brew to install following:</p>

<ul>
<li>python - 2.7.12_2</li>
<li>bazel - 0.3.2</li>
<li>protobuf - 3.1.0</li>
</ul>

<p>Then I used Pip to install CPU only TensorFlow from:
<a href="https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl" rel="nofollow">https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-0.11.0rc0-py2-none-any.whl</a></p>

<p>I checked out the Magenta project from: <a href="https://github.com/tensorflow/magenta" rel="nofollow">https://github.com/tensorflow/magenta</a>
and run all the test using:</p>

<pre><code>bazel test //magenta/...
</code></pre>

<p>And all of them have passed.</p>

<p>So far so good. So I decided to give the GPU enabled version of TensorFlow a shot and installed it from:
<a href="https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.11.0rc0-py2-none-any.whl" rel="nofollow">https://storage.googleapis.com/tensorflow/mac/gpu/tensorflow-0.11.0rc0-py2-none-any.whl</a></p>

<p>Now all the tests fail with the following error:</p>

<pre><code>import tensorflow as tf
  File "/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py", line 23, in &lt;module&gt;
    from tensorflow.python import *
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in &lt;module&gt;
    from tensorflow.python import pywrap_tensorflow
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in &lt;module&gt;
    _pywrap_tensorflow = swig_import_helper()
  File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 24, in swig_import_helper
    _mod = imp.load_module('_pywrap_tensorflow', fp, pathname, description)
ImportError: dlopen(/usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so, 10): Library not loaded: @rpath/libcudart.7.5.dylib
  Referenced from: /usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so
  Reason: image not found
</code></pre>

<p>So obviously the script run from Bazel has trouble locating the libcudart.7.5.dylib library.</p>

<p>I did try running GPU computations from Python without Bazel and everything seems to be fine.</p>

<p>I also did create a test script and run it using Bazel and it seems that the directory containing libcudart.7.5.dylib library is reachable, however the LD_LIBRARY_PATH is not set.</p>

<p>I searched the documentation and found --action_env and --test_env flags, but none of them actually seems to set the LD_LIBRARY_PATH for the execution.</p>

<p>These are the options from loaded from .bazelrc files.</p>

<pre><code>Inherited 'common' options: --isatty=1 --terminal_columns=80
Inherited 'build' options: --define=allow_oversize_protos=true --copt -funsigned-char -c opt --spawn_strategy=standalone
'run' options: --spawn_strategy=standalone
</code></pre>

<p>What is the correct way to let Bazel know about the runtime dependencies?</p>

<p>Use </p>

<pre><code>export LD_LIBRARY_PATH=/usr/local/cuda/lib64/
</code></pre>

<p>before launching bazel. Double check in the directory above if there is such a file.</p>

<pre><code>ls /usr/local/cuda/lib64/libcudart.7.5.dylib 
</code></pre>

<p>Note that in Macosx the name is different:</p>

<pre><code>export DYLD_LIBRARY_PATH=/usr/local/cuda/lib/
</code></pre>

<p>See this answer for more information on <a href="http://superuser.com/questions/282450/where-do-i-set-dyld-library-path-on-mac-os-x-and-is-it-a-good-idea">SuperUser</a></p>

python osx tensorflow bazel