Tensorflow 2.0 – AttributeError: module “tensorflow” has no attribute “Session”


When I am executing the command sess = tf.Session() in Tensorflow 2.0 environment, I am getting an error message as below:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module "tensorflow" has no attribute "Session"

System Information:

  • OS Platform and Distribution: Windows 10
  • Python Version: 3.7.1
  • Tensorflow Version: 2.0.0-alpha0 (installed with pip)

Steps to reproduce:


  1. pip install --upgrade pip
  2. pip install tensorflow==2.0.0-alpha0
  3. pip install keras
  4. pip install numpy==1.16.2


  1. Execute command: import tensorflow as tf
  2. Execute command: sess = tf.Session()

Answer rating: 299

According to TF 1:1 Symbols Map, in TF 2.0 you should use tf.compat.v1.Session() instead of tf.Session()


To get TF 1.x like behaviour in TF 2.0 one can run

import tensorflow.compat.v1 as tf

but then one cannot benefit of many improvements made in TF 2.0. For more details please refer to the migration guide https://www.tensorflow.org/guide/migrate

Answer rating: 75

TF2 runs Eager Execution by default, thus removing the need for Sessions. If you want to run static graphs, the more proper way is to use tf.function() in TF2. While Session can still be accessed via tf.compat.v1.Session() in TF2, I would discourage using it. It may be helpful to demonstrate this difference by comparing the difference in hello worlds:

TF1.x hello world:

import tensorflow as tf
msg = tf.constant("Hello, TensorFlow!")
sess = tf.Session()

TF2.x hello world:

import tensorflow as tf
msg = tf.constant("Hello, TensorFlow!")

For more info, see Effective TensorFlow 2