Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@Atlas7
Last active January 3, 2018 09:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Atlas7/027de1c7ebbdcadedc4916f4d28cc213 to your computer and use it in GitHub Desktop.
Save Atlas7/027de1c7ebbdcadedc4916f4d28cc213 to your computer and use it in GitHub Desktop.
How to import tensorflow from python console, Jupyter console / qtconsole / notebook, and ipython notebook?

(keeping notes here for ease of future references)

Background

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

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/gpu_device.cc:977] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 750 Ti, pci bus id: 0000:01:00.0)

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