9

Apologies in advance, I think the issue is quite perplexing!

I would like to use TensorFlow through Jupyter, with a Python3 kernel.

However the command import tensorflow as tf returns the error: ImportError: No module named tensorflow when either Python2 or Python3 is specified as the Jupyter kernel.

  • I have Python 2 and Python 3 installed on my Mac and can access both versions through Terminal.
  • I installed TensorFlow for Python 3, however I can only access it via Python 2 on the Terminal.

As such, this question is really two-fold:

  1. I want to get TensorFlow working with Python3
  2. ...which should lead to TensorFlow working with Jupyter on the Python3 terminal.
1
  • did you install tensorflow? are you able to run import tensorflow from a python interpreter ? Commented Mar 6, 2016 at 1:29

1 Answer 1

17

I had the same problem and solved it using the tutorial Using a virtualenv in an IPython notebook. I'll walk you through the steps I took.

I am using Anaconda, and I installed a new environment tensorflow using these instructions at tensorflow.org. After that, here is how I got tensorflow to work in a Jupyter notebook:

  1. Open Terminal
  2. Run source activate tensorflow. You should now see (tensorflow) at the beginning of the prompt.
  3. Now that we are in the tensorflow environment, we want to install ipython and jupyter in this environment: Run

    conda install ipython 
    

    and

    conda install jupyter
    
  4. Now follow the instructions in the tutorial linked above. I'll repeat them here with a bit more information added. First run

    ipython kernelspec install-self --user 
    

    The result for me was Installed kernelspec python3 in /Users/charliebrummitt/Library/Jupyter/kernels/python3

  5. Run the following:

    mkdir -p ~/.ipython/kernels
    

    Then run the following with <kernel_name> replaced by a name of your choice (I chose tfkernel) and replace the first path (i.e., ~/.local/share/jupyter/kernels/pythonX) by the path generated in step 4:

    mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>
    
  6. Now you'll see a new kernel if you open a Jupyter notebook and select Kernel -> Change kernel from the menu. But the new kernel will have the same name as your previous kernel (for me it was called Python 3). To give your new kernel a unique name, run in Terminal

    cd ~/.ipython/kernels/tfkernel/
    

    and then run vim kernel.json to edit the file kernel.json so that you replace the value of "display_name" from the default (Python 3) to a new name (I chose to call it "tfkernel"). Save and exit vim by typing :wq while in command mode.

  7. Open a new Jupyter notebook and type import tensorflow as tf. If you didn't get ImportError then you are ready to go!
Sign up to request clarification or add additional context in comments.

4 Comments

this would be quite useful if translated to "windows" language. Many corporations in industry use/require the 2nd most recent Microsoft operating system.
Such a lifesaver..I was able to import tensorflow when running in RHEL without "conda install jupyter/ipython" once I source avtivated tensorflow. As I moved into AWS Linuximage, it required me to do this step. Thanks for sharing. I did not do the "ipython kernelspec install-self --user" but I will keep it to have all binaries loaded up
Thanks, but followed all your instructions on Mac El Capitan (many times!) and still ImportError exception is thrown. It's importing tf in the python's CLI, but no luck in the Notebooks!! Was trying for the last 3 hours, maybe it's time to give up..
I do not have a ~/.local/share/jupyter folder, but I was able to follow all of the steps until 5. the ipython kernelspec was installed in /Users/amc/Library/Jupyter/kernels/python2... what should I do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.