Solving AttributeError: module “tensorflow” has no attribute “Session” error

This blog will answer how to solve AttributeError: module “tensorflow” has no attribute “Session” in Python. Is it because the module is missing? Or is it because there is a Python version conflict? So we will try to give the solutions for this problem.

AttributeError: module “tensorflow” has no attribute “Session”

When Does This Happen?

We are trying to run a code, and the error appears when using tensorflow that says this session is not working, as demonstrated below.

“AttributeError: module “tensorflow” has no attribute “Session”.

The tensorflow has launched the 2.0 version to be more accurate. The “tf. Session()” feature may not be present in the most recent version of Tensorflow 2.0, which might explain this problem.

How To Solve The AttributeError: module “tensorflow” has no attribute “Session” Problem

When using Tensorflow version 2.0, add “compat v1” into the session. So instead of tf.Session(), we use tf.compat.v1.Session(). Another thing is that the program discontinued session () in TF 2.0, so you may make use of tf.compat.v1.Session ().

To solve this problem, first, we must import the Tensorflow as tf where tf replaces it later on. Next, we’ll make a variable out of it and designate a tf constant() code. 

This method in Python accepts a constant number that symbolizes the value which does not change and initializes an object such as arrays or lists.

The Solutions

Solution 1

Apply with compat v1 in the session as shown below if running TF 2.0.

“tf.compat.v1.Session()”
instead of
“tf.Session()”

Solution 2

Apply this command if you’re running TF 1X. First, we imported the Tensorflow and replaced the name as tf. Then, we made a new variable and created a new session below.

“import tensorflow as tf
msg = tf.constant('Hello world!!')
sess = tf.Session()”

Solution 3

As in the below solution, we utilized the tf.compat.v1.disable_eager_execution() method, which is simpler since it executes the tasks without constructing graphs. This function is included in TF 2.0.
“import tensorflow as tf
tf.compat.v1.disable_eager_execution()
arg = tf.constant('Hello, World!!')
sess = tf.compat.v1.Session()
print(sess.run(arg))”

Conclusion

Were our solutions on AttributeError: module “tensorflow” has no attribute “Session” have supported you a lot for your problem? If so, you may provide us with your thoughts and ideas in the comment section to understand your problem better.

Also, if you have any concerns during your work, feel free to chat with other members or us for more support to find out which solution may best suit you. Thank you for reading!


Related posts
Scroll to Top