Tensorflow 1.0.1 with Jupyter in virtualenv

2 minute read

I recently upgraded to the latest release of TensorFlow (v1.0.1) and it took me some time to get it up and running with Jupyter within a virtual environment. Here is a step-by-step guide to avoid some of the traps. These steps are for installing the CPU version of tensorflow v1.0.1 on MacOSX running Python 2.7

Step 1: Create a virtual environment and install the latest release

Follow steps 1 to 4 for installing with virtualenv in the TensorFlow documentation. If you have a previous version of tensorflow in a virtualenv environment and want to upgrade to the latest one, I would recommend creating a new virtualenv environment and a fresh installation.

After activating the virtualenv environment, you can run pip install --upgrade tensorflow. If it throws an error, as it did for me, you can provide the url for the latest release of the tensorflow package as below

pip install --upgrade https://storage.googleapis.com/tensorflow/mac/cpu/tensorflow-1.0.1-py2-none-any.whl

Invoke python on the terminal, and in the python shell check to see if you are able to import tensorflow and also its version

import tensorflow as tf
tf.__version__

If everything works fine, then you are set up with tensorflow v1.0.1

Step 2: Getting Jupyter to run with TensorFlow

The best way to check if this is even an issue is to start jupyter notebook, and in a new notebook, run the python code at the end of Step 1 to import tensorflow and check its version. If that matches the output of your python shell from before, you are all set. If it is unable to find tensorflow or refers to an older version, you may want to follow the steps below.

I use this stackoverflow response as a reference. Type the following commands on your terminal

which python

This command should point to [location of your virtualenv]\bin\python from where your virtualenv invokes python

Next check ipython and jupyter

which ipython
which jupyter

If this points to a location other than [location of your virtualenv]\bin\ipython (for ipython) or [location of your virtualenv]\bin\jupyter (for jupyter), then you need to install ipython and jupyter on your virtualenv environment.

You can do this using the following commands (while in your tensorflow virtualenv environment)

pip install --ignore-installed ipython
pip install --upgrade jupyter

Note that it is important to install ipython before installing jupyter as above, so jupyter can recognize your python kernel.

Run the which ipython and which jupyter commands again and see if it has updated the location to point to the bin folder within your virtualenv environment location. If not, then deactivate your virtualenv and activate again and run the which commands to see if it has updated

Once you have jupyter set up on your virtualenv, you can check again to see if you are able to import tensorflow within the notebook and that it is the right version.

Updated:

Leave a Comment