Instantly share code, notes, and snippets.

@Atlas7 / Secret
Last active Jan 3, 2018

What would you like to do?
How to import tensorflow from python console, Jupyter console / qtconsole / notebook, and ipython notebook?

(keeping notes here for ease of future references)


See this Stackoverflow forum - Trouble with TensorFlow in Jupyter Notebook

This post is a copy of the answer I submitted (for ease of my own references). i.e. create an environment.yml file to make this conda / tensorflow installation process more repeatable.

Acknowledgement: The accepted answer (by Zhongyu Kuang) did help me out tons.

Step 1 - create a Conda environment.yml File

environment.yml looks like this:

name: hello-tensorflow
  - python=3.6
  - jupyter
  - ipython
  - pip:


  • 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

which jupyter...

(hello-tensorflow) $ which jupyter

which ipython...

(hello-tensorflow) $ which ipython

Step 5

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

Here is what I get:

In [1]: import tensorflow as tf

In [2]: hello = tf.constant('Hello, TensorFlow!')

In [3]: sess = tf.Session()
2017-05-31 17:16:41.853631: I tensorflow/core/common_runtime/gpu/] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 750 Ti, pci bus id: 0000:01:00.0)

In [4]: print(
b'Hello, TensorFlow!'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment