notebook: Unable to import tensorflow in jupyter (yet able to import tensorflow in python and ipython)
I am using OSX 10.11 with homebrew python, and tensorflow GPU with cuda and cudnn installed.
Currently tensorflow imports correctly in both python and ipython:
Python 2.7.13 (default, Dec 17 2016, 23:03:43)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcublas.dylib locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcudnn.dylib locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcufft.dylib locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcuda.1.dylib locally
I tensorflow/stream_executor/dso_loader.cc:128] successfully opened CUDA library libcurand.dylib locally
>>>
Yet I am unable to import tensorflow in jupyter (I have tried both Jupyter notebook and Jupyter console):
Jupyter console 5.0.0
Python 2.7.13 (default, Dec 17 2016, 23:03:43)
Type "copyright", "credits" or "license" for more information.
IPython 5.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import tensorflow
---------------------------------------------------------------------------
ImportError Traceback (most recent call last)
<ipython-input-1-a649b509054f> in <module>()
----> 1 import tensorflow
/usr/local/lib/python2.7/site-packages/tensorflow/__init__.py in <module>()
22
23 # pylint: disable=wildcard-import
---> 24 from tensorflow.python import *
25 # pylint: enable=wildcard-import
26
/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py in <module>()
58 please exit the tensorflow source tree, and relaunch your python interpreter
59 from there.""" % traceback.format_exc()
---> 60 raise ImportError(msg)
61
62 # Protocol buffers
ImportError: Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 49, in <module>
from tensorflow.python import pywrap_tensorflow
File "/usr/local/lib/python2.7/site-packages/tensorflow/python/pywrap_tensorflow.py", line 28, in <module>
_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.8.0.dylib
Referenced from: /usr/local/lib/python2.7/site-packages/tensorflow/python/_pywrap_tensorflow.so
Reason: image not found
Error importing tensorflow. Unless you are using bazel,
you should not try to import tensorflow from its source directory;
please exit the tensorflow source tree, and relaunch your python interpreter
from there.
I have tried adding the following in ~/.bashrc and ~/.bash_profile and /etc/profile , yet it still does not work… export CUDA_HOME=/usr/local/cuda export DYLD_LIBRARY_PATH=“$CUDA_HOME/lib:$DYLD_LIBRARY_PATH” export PATH=“$CUDA_HOME/bin:$PATH”
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 1
- Comments: 31 (11 by maintainers)
Try running the following in both the notebook and ipython:
Odds are they are using different python executables. If this is the case, you can update your python kernelspec. See https://github.com/jupyter/notebook/issues/397 for reference.
Are you launching the Jupyter applications from a GUI launcher? If so, they won’t pick up environment variables set in
.bashrcor.bash_profile. A simple workaround is to launch them from a terminal.On Linux, you can set environment variables in
.profile, which is run when you log in. I don’t know how to do the same on OSX.@rylanchiu First, run
jupyter kernelspec listand you will see an output like:To edit the spec for
python3, let’s see which files are in its directory by runningls /usr/local/share/jupyter/kernels/python3:Lastly, we want to open
kernel.jsonin our text editor, there are several ways to do this:atom /usr/local/share/jupyter/kernels/python3/kernel.json.jsonfiles:open /usr/local/share/jupyter/kernels/python3/kernel.jsonvim /usr/local/share/jupyter/kernels/python3/kernel.jsonI had a similar issue. It could be that your Jupyter notebook is looking into a different path than where you have installed tensorflow. You want to make sure Jupyter opens the correct Python through the correct kernel. One way to fix this is to install nb_conda by typing the following in your terminal:
conda install -c anaconda-nb-extensions nb_condaYou can then run Jupyter and when selecting a notebook from the New dropdown menu, make sure you select the Python that is connected to the kernel you want (kernel where you have all the libraries and dependencies such as tensorflow)
I am launching from terminal, with the command
jupyter-notebook --no-browser.Ok, to find the location of your kernelspecs:
jupyter kernelspec listThen, edit your
pythonkernelspec in your editor. I think the easiest fix is to rename your python executable from an absolute path (e.g. “/Users/grant/anaconda/bin/python”) to just “python”.I am on Windows 10 and using Tensorflow (tf) 1.13. I had this problem as well. I had tf installed (using conda) into a conda virtual env (v-env). I installed
ipykerneland made it available within the notebook. However, I couldn’t import tf from my notebook.Here is how I resolved it.
Summary: - Create a conda virtual env - Install tensorflow (into the v-env) using conda - Install
ipykernelinto the v-env and install it. Help hereI have tried your way @gnestor, unfortunately, it did not work. So I kept searching and found an answer from StackOverflow (https://stackoverflow.com/questions/37061089/trouble-with-tensorflow-in-jupyter-notebook). After following the steps shown in the answer and changed python executable python, as you recommended, from “/Users/username/anaconda/bin/python” to just “/Users/username/anaconda/envs/virtualproject/bin/python”, the jupyter notebook successfully imported tensorflow!
@gnestor Thanks for your detailed and helpful instruction! It solved my problem perfectly!
Yes! that worked thanks
I solved the problem by adding
"env":{"LD_LIBRARY_PATH":"/usr/local/cuda-9.0/lib64:[...]"to my envirnment’s kernelspec. As mentioned above, the spec directory can be found withjupyter kernelspec listFWIW on OSX, this helped me: https://github.com/jupyter/jupyter/issues/245#issuecomment-286927371
@takluyver Thanks. As I am running python 2.7 I couldn’t run it. However, I imported os and checked os.environ, and I found I have ‘DYLD_LIBRARY_PATH’ in my python os.environ, but not in my Jupyter console os.environ.
So this must be the cause. But I am not sure how to solve it.
Did you install tensorflow from source? Are you running jupyter from the ts source directory?