Diagram
AttributeError: module ‘tensorflow’ has no attribute ‘Session’ error occurs because you are trying to “access the Session attribute in TensorFlow, but the TensorFlow version you are using doesn’t have this attribute.” You are using TensorFlow 2.x, which has a different architecture and API than TensorFlow 1.x.
The main reason for the error is that if you are using an old version of TensorFlow or if you are using a newer version of TensorFlow but you are trying to use an API that is no longer supported.
In TensorFlow 2.x, the need for a Session to run the computation graph has been eliminated, and eager execution is enabled by default. Therefore, the tf.Session attribute is not present in TensorFlow 2.x.
How to fix the AttributeError: module ‘tensorflow’ has no attribute ‘Session’
Here are three ways to fix the AttributeError: module ‘tensorflow’ has no attribute ‘Session’ error.
- Using the Eager Execution Mode
- Using TensorFlow 2.x (Functional API) and Remove Session Object
- Using the Compatibility Module
Solution 1: Using the Eager Execution Mode
Eager execution allows you to run TensorFlow operations immediately, as they are called, rather than building a computational graph to run later.
import tensorflow as tf
# Create a constant tensor
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
# Perform operations directly
c = a + b
print(c)
Output
tf.Tensor([4. 6.], shape=(2,), dtype=float32)
Solution 2: Using TensorFlow 2.x (Functional API) and Remove Session Object
The Session object is no longer needed in Tensorflow 2.0. Therefore, you need to remove any code that uses the Session object.
import tensorflow as tf
# Define the computational graph
a = tf.constant(2)
b = tf.constant(3)
c = tf.add(a, b)
# Run the computational graph
result = c.numpy()
# Print the result
print(result)
Output
5
Solution 3: Using the Compatibility Module
If you have legacy code written for TensorFlow 1.x and don’t want to rewrite it, you can still run it in TensorFlow 2.x using the tf.compat.v1 API.
import tensorflow as tf
# Disable eager execution
tf.compat.v1.disable_eager_execution()
# Create a session
sess = tf.compat.v1.Session()
# Define computation graph
a = tf.constant([1.0, 2.0])
b = tf.constant([3.0, 4.0])
c = a + b
# Run the session
result = sess.run(c)
print(result)
# Close the session
sess.close()
Output
[4. 6.]
Choose the approach that best suits your needs. If you’re starting a new project, it’s highly recommended to use TensorFlow 2.x APIs.
Related posts
AttributeError: module ‘tensorflow’ has no attribute ‘log’
AttributeError: module ‘tensorflow’ has no attribute ‘GPUOptions’
AttributeError: module ‘tensorflow’ has no attribute ‘gfile’
AttributeError: module ‘tensorflow’ has no attribute ‘reset_default_graph’
AttributeError: Module ‘tensorFlow’ has no attribute ‘set_random_seed’
AttributeError: module ‘tensorflow’ has no attribute ‘test’
AttributeError: module ‘tensorflow’ has no attribute ‘variable_scope’
AttributeError: module ‘tensorflow’ has no attribute ‘layers’
AttributeError: module ‘tensorflow’ has no attribute ‘ConfigProto’

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.