3

I am writing the following code in Jupyter notebook.

import tensorflow as tf
g = tf.Graph()

And I encounter the following error while executing this simple code.

AttributeError: module 'tensorflow' has no attribute 'Graph'

If I execute the same from the console. It works. Any idea what's happening here.

More information:(test is my virtualenv)

ipython, jupyter, python version:

(test) xingzhou@xingzhou008:~/tensorflow$ which ipython3
/home/xingzhou/tensorflow/test/bin/ipython3
(test) xingzhou@xingzhou008:~/tensorflow$ which jupyter
/home/xingzhou/tensorflow/test/bin/jupyter
(test) xingzhou@xingzhou008:~/tensorflow$ which python3
/home/xingzhou/tensorflow/test/bin/python3

Execute from Console:

(test) xingzhou@xingzhou008:~/tensorflow$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
>>> g = tf.Graph()
>>> type(g)
<class 'tensorflow.python.framework.ops.Graph'>
>>> 
Xing Zhou
  • 71
  • 1
  • 1
  • 6
  • [AttributeError: module 'tensorflow' has no attribute 'reset_default_graph'](https://stackoverflow.com/a/51120550/6521116) – LF00 Jul 01 '18 at 05:05

4 Answers4

3

Make sure you haven't called any of your python files as 'tensorflow.py'

TomV
  • 1,157
  • 1
  • 13
  • 25
2

I resolved this issue by restarting Jupyter Notebook kernel. It's weird.

Xing Zhou
  • 71
  • 1
  • 1
  • 6
1

A simple solution worked for me. From anaconda navigator first go to environments and create a tensorflow environment like this image

enter image description here

I named it tensorflow. Then from the right select all from the drop down menu and search for tensorflow.

enter image description here

Select it and hit apply. It will download some additional packages along with tensorflow. When you are done, select tensorflow from home and you should be good to go. Here it works.

enter image description here

A.A Noman
  • 5,244
  • 9
  • 24
  • 46
0

Check if TensorFlow is installed in Anaconda/Environments. If not, just search and install. Then try to run g=tf.Graph() again. It worked for me.