Trouble with TensorFlow in Jupyter Notebook

Question:

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.

Asked By: Surgical Commander

||

Answers:

Your Anaconda install probably went to different directory than your Python install

For instance on my machine I can find location here

yaroslavvb-macbookpro:~ yaroslavvb$ which ipython
/Users/yaroslavvb/anaconda/bin/ipython

When you type python, it tries to find it in PATH going in left-to-right order. So you may have another version of python in a folder before Anaconda folder, and it’ll use that. To fix, you can do export PATH=.... to change the path, and put Anaconda directory in front, so that it takes python from there instead of the default, ie

export PATH=/Users/yaroslavvb/anaconda/bin:$PATH
Answered By: Yaroslav Bulatov

I think your question is very similar with the question post here. Windows 7 jupyter notebook executing tensorflow. As Yaroslav mentioned, you can try

conda install -c http://conda.anaconda.org/jjhelmus tensorflow .

Answered By: Yan Zhao

I installed PIP with Conda conda install pip instead of apt-get install python-pip python-dev.

Then installed tensorflow
use Pip Installation:

# Ubuntu/Linux 64-bit, CPU only, Python 2.7 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl
# Ubuntu/Linux 64-bit, GPU enabled, Python 2.7 
# Requires CUDA toolkit 7.5 and CuDNN v4. For other versions, see "Install from sources" below. 
$ export TF_BINARY_URL=https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.9.0-cp27-none-linux_x86_64.whl

pip install --upgrade $TF_BINARY_URL

Then it will work in jupyter notebook.

Answered By: lucky6qi

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

Answered By: Zhongyu Kuang

I have another solution that you don’t need to source activate tensorflow before using jupyter notebook every time.

Partion 1

Firstly, you should ensure you have installed jupyter in your virtualenv. If you have installed, you can skip this section (Use which jupyter to check). If you not, you could run source activate tensorflow, and then install jupyter in your virtualenv by conda install jupyter. (You can use pip too.)

Partion 2

1.From within your virtualenv, run

username$ source activate tensorflow
(tensorflow)username$ ipython kernelspec install-self --user

This will create a kernelspec for your virtualenv and tell you where it is:

(tensorflow)username$ [InstallNativeKernelSpec] Installed kernelspec pythonX in /home/username/.local/share/jupyter/kernels/pythonX

Where pythonX will match the version of Python in your virtualenv.

2.Copy the new kernelspec somewhere useful. Choose a kernel_name for your new kernel that is not python2 or python3 or one you’ve used before and then:

(tensorflow)username$ mkdir -p ~/.ipython/kernels
(tensorflow)username$ mv ~/.local/share/jupyter/kernels/pythonX ~/.ipython/kernels/<kernel_name>

3.If you want to change the name of the kernel that IPython shows you, you need to edit ~/.ipython/kernels/<kernel_name>/kernel.json and change the JSON key called display_name to be a name that you like.

4.You should now be able to see your kernel in the IPython notebook menu: Kernel -> Change kernel and be able so switch to it (you may need to refresh the page before it appears in the list). IPython will remember which kernel to use for that notebook from then on.

Reference.

Answered By: user5746429

I had a similar issue when using a custom Ubuntu 16 image. The problem was related to an existing version of numpy that was already installed on my system.

I initially tried

sudo pip3 install tensorflow

This resulted in the following exception:

Exception:
Traceback (most recent call last):
File “/anaconda/envs/py35/lib/python3.5/shutil.py”, line 538, in move
os.rename(src, real_dst)
PermissionError: [Errno 13] Permission denied: ‘/anaconda/envs/py35/lib/python3.5/site-packages/numpy’ -> ‘/tmp/pip-co73r3hm-uninstall/anaconda/envs/py35/lib/python3.5/site-packages/numpy’

The docs advise that if you encounter any issues with this command to try the following:

sudo pip3 install --upgrade  
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl

However, my system was unable to locate pip3

sudo: pip3 command not found

The ulitmate solution was to create a symlink for pip3

sudo ln -s /anaconda/envs/py35/bin/pip3.5 /usr/local/bin/pip3

Finally, the following command worked without trouble

sudo /usr/local/bin/pip3 install --upgrade  
https://storage.googleapis.com/tensorflow/linux/cpu/tensorflow-1.0.1-cp35-cp35m-linux_x86_64.whl

I verified the installation in the terminal and also verified a successful import in my Jupyter Notebook

import tensorflow as tf
Answered By: Brian

The accepted answer (by Zhongyu Kuang) has just helped me out. Here I’ve create an environment.yml file that enables me to make this conda / tensorflow installation process repeatable.

Step 1 – create a Conda environment.yml File

environment.yml looks like this:

name: hello-tensorflow
dependencies:
  - python=3.6
  - jupyter
  - ipython
  - pip:
    - https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow_gpu-1.1.0-cp36-cp36m-linux_x86_64.whl

Note:

  • Simply replace the name to whatever you want. (mine is hello-tensorflow)
  • Simply replace the python version to whatever you want. (mine is 3.6)
  • Simply replace the tensorflow pip install URL to whatever you want (mine is the Tensorflow URL where Python 3.6 with GPU support)

Step 2 – create the Conda environment

With the environment.yml being in the current path you are on, this command creates the environment hello-tensorflow (or whatever you have renamed it to):

conda env create -f environment.yml

Step 3: source activate

Activate the newly created environment:

source activate hello-tensorflow

Step 4 – which python / jupyter / ipython

which python…

(hello-tensorflow) $ which python
/home/johnny/anaconda3/envs/hello-tensorflow/bin/python

which jupyter…

(hello-tensorflow) $ which jupyter
/home/johnny/anaconda3/envs/hello-tensorflow/bin/jupyter

which ipython…

(hello-tensorflow) $ which ipython
/home/johnny/anaconda3/envs/hello-tensorflow/bin/ipython

Step 5

You should now be able to import tensorflow from python, jupyter (console / qtconsole / notebook, etc.) and ipython.

Answered By: Atlas7

Here is what I did to enable tensorflow in Anaconda -> Jupyter.

  1. Install Tensorflow using the instructions provided at
  2. Go to /Users/username/anaconda/env and ensure Tensorflow is installed
  3. Open the Anaconda navigator and go to “Environments” (located in the left navigation)
  4. Select “All” in teh first drop down and search for Tensorflow
  5. If its not enabled, enabled it in the checkbox and confirm the process that follows.
  6. Now open a new Jupyter notebook and tensorflow should work
Answered By: Ankur Shrivastav

i used these following which in virtualenv.

pip3 install --ignore-installed ipython
pip3 install --ignore-installed jupyter

This re-installs both ipython and jupyter notebook in my tensorflow virtual environment. You can verify it after installation by which ipython and which jupyter. The bin will be under the virtual env.

NOTE I am using python 3.*

Answered By: as – if

I wonder if it is not enough to simply launch ipython from tensorflow environnement. That is
1) first activate tensorflow virtualenv with:

source ~/tensorflow/bin/activate

2) launch ipython under tensorflow environnement

(tensorflow)$ ipython notebook --ip=xxx.xxx.xxx.xxx
Answered By: sph

I found the solution from someone else’s post. It is simple and works well!

http://help.pythonanywhere.com/pages/IPythonNotebookVirtualenvs

Just install the following in the Command Prompt and change kernel to Python 3 in Jupyter Notebook. It will import tensorflow successfully.

pip install tornado==4.5.3

pip install ipykernel==4.8.2

(Orginial post: https://github.com/tensorflow/tensorflow/issues/11851)

Answered By: LovelyVV

Jupyter Lab: ModuleNotFound tensorflow

For a future version of me or a colleague that runs into this issue:

conda install -c conda-forge jupyter jupyterlab keras tensorflow

Turns out jupyterlab is a plugin for jupyter.

So even if you are in an environment that has jupyter but not jupyterlab as well, if you try to run:

jupyter lab

then jupyter will look in the (base) environment for the jupyterlab plugin.

Then your imports in jupyter lab will be relative to that plugin and not your conda environment.

Answered By: Josh Peak

Open an Anaconda Prompt screen: (base) C:UsersYOU>conda create -n tf tensorflow

After the environment is created type: conda activate tf

Prompt moves to (tf) environment, that is: (tf) C:UsersYOU>

then install Jupyter Notebook in this (tf) environment:
conda install -c conda-forge jupyterlab - jupyter notebook

Still in (tf) environment, that is type
(tf) C:UsersYOU>jupyter notebook

The notebook screen starts!!

A New notebook then can import tensorflow

FROM THEN ON
To open a session
click Anaconda prompt,
type conda activate tf

the prompt moves to tf environment
(tf) C:UsersYOU>

then type (tf) C:UsersYOU>jupyter notebook

Answered By: EduardoriosChicago
pip install tensorflow

This worked for me in my conda virtual environment.

I was trying to use conda install tensorflow in a conda virtual environment where jupyter notebooks was already installed, resulting in many conflicts and failure. But pip install worked fine.

Answered By: Jonathan Vance

conda info –envs

conda create –name py3-TF2.0 python=3

Proceed ([y]/n)? y

conda activate py3-TF2.0

conda install tensorflow

pip install –upgrade tensorflow

conda install -c anaconda ipykernel

python -m ipykernel install –user –name=py3-TF2.0

it is the best way but if it is required you should upgrade numpy with scipy as well

Answered By: GoDeeply

You can try "conda install tensorflow". This will install TensorFlow in your Anaconda directory.
Your local pip directory might not be shared with the Anaconda directory.

Thanks!

Answered By: Pratap
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.