pythontensorflowjupyter

Trouble with TensorFlow in Jupyter Notebook


I installed Jupyter notebooks in Ubuntu 14.04 via Anaconda earlier, and just now I installed TensorFlow. I would like TensorFlow to work regardless of whether I am working in a notebook or simply scripting. In my attempt to achieve this, I ended up installing TensorFlow twice, once using Anaconda, and once using pip. The Anaconda install works, but I need to preface any call to python with "source activate tensorflow". And the pip install works nicely, if start python the standard way (in the terminal) then tensorflow loads just fine.

My question is: how can I also have it work in the Jupyter notebooks?

This leads me to a more general question: it seems that my python kernel in Jupyter/Anaconda is separate from the python kernel (or environment? not sure about the terminology here) used system wide. It would be nice if these coincided, so that if I install a new python library, it becomes accessible to all the varied ways I have of running python.


Solution

  • Update

    TensorFlow website supports five installations.

    To my understanding, using Pip installation directly would be fine to import TensorFlow in Jupyter Notebook (as long as Jupyter Notebook was installed and there were no other issues) b/z it didn't create any virtual environments.

    Using virtualenv install and conda install would need to install jupyter into the newly created TensorFlow environment to allow TensorFlow to work in Jupyter Notebook (see the following original post section for more details).

    I believe docker install may require some port setup in the VirtualBox to make TensorFlow work in Jupyter Notebook (see this post).

    For installing from sources, it also depends on which environment the source code is built and installed into. If it's installed into a freshly created virtual environment or an virtual environment which didn't have Jupyter Notebook installed, it would also need to install Jupyter Notebook into the virtual environment to use Tensorflow in Jupyter Notebook.

    Original Post

    To use tensorflow in Ipython and/or Jupyter(Ipython) Notebook, you'll need to install Ipython and Jupyter (after installing tensorflow) under the tensorflow activated environment.

    Before install Ipython and Jupyter under tensorflow environment, if you do the following commands in terminal:

    username$ source activate tensorflow
    
    (tensorflow)username$ which ipython
    (tensorflow)username$ /Users/username/anaconda/bin/ipython
    
    (tensorflow)username$ which jupyter
    (tensorflow)username$ /Users/username/anaconda/bin/jupyter
    
    (tensorflow)username$ which python
    (tensorflow)username$ /User/username//anaconda/envs/tensorflow/bin/python
    

    This is telling you that when you open python from terminal, it is using the one installed in the "environments" where tensorflow is installed. Therefore you can actually import tensorflow successfully. However, if you are trying to run ipython and/or jupyter notebook, these are not installed under the "environments" equipped with tensorflow, hence it has to go back to use the regular environment which has no tensorflow module, hence you get an import error.

    You can verify this by listing out the items under envs/tensorflow/bin directory:

    (tensorflow) username$ ls /User/username/anaconda/envs/tensorflow/bin/
    

    You will see that there are no "ipython" and/or "jupyer" listing out.

    To use tensorflow with Ipython and/or Jupyter notebook, simply install them into the tensorflow environment:

    (tensorflow) username$ conda install ipython
    (tensorflow) username$ pip install jupyter #(use pip3 for python3)
    

    After installing them, there should be a "jupyer" and a "ipython" show up in the envs/tensorflow/bin/ directory.

    Notes: Before trying to import tensorflow module in jupyter notebook, try close the notebook. And "source deactivate tensorflow" first, and then reactivate it ("source activate tensorflow") to make sure things are "on the same page". Then reopen the notebook and try import tensorflow. It should be import successfully (worked on mine at least).