In this Python tutorial, we will discuss the error “**module ‘TensorFlow’ has no attribute ‘session’**“. And we’ll also cover the following topics:

- attributeerror module ‘tensorflow.keras.backend’ has no attribute ‘get_session’
- attributeerror module ‘tensorflow.compat.v2’ has no attribute ‘session’

**Table of Contents**show

## AttributeError “module ‘Tensorflow’ has no attribute ‘session'”

- In this section, we will discuss the error
**AttributeError:”module ‘Tensorflow’ has no attribute ‘session’**in Python. - To do this task first we will import the TensorFlow library with tf alias where tf represents the TensorFlow and it is used for numerical computation problems. Next, we will create a variable in the form of tensors and assign a
**tf.constant()**function. In Python this function takes a constant value that represents the value does not modify and it also initialized an object like an array or list. - In the given example we have assigned the scaler value and datatype as an argument. Now we are going to multiply the given two variables named
**‘l’**and**‘m’**. To do this task first we learn the concept of**tf.session()**. In Python, this function is used to perform some operations in graphs. It will check the nodes of graphs and in this example, we will create the session like**tf.session()**and launch the session with**tf.session()**as Val where Val is the session name. - Here we are going to apply the mathematical operation
**(multiplication)**at node**new_output**. To run this session we will use the**val.run()**syntax within this argument we will use the multiplication operation in it.

**Example:**

```
import tensorflow as tf
l = tf.constant(23,dtype="int32",name="val1")
m = tf.constant(22,dtype="int32",name="val2")
with tf.Session() as val:
new_output=val.run(l*m)
print(new_output)
```

Here is the Screenshot of the following given code

As you can see in the Screenshot the output displays the error** AttributeError: module ‘TensorFlow’** has no attribute ‘session’.

**Reason: **The possible reason for this error is that the tf.session() attribute is not available in Tensorflow’s latest version (TensorFlow2.0).

Now let’s see the solution to this

**Solution:**

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
l = tf.constant(23,dtype="int32",name="val1")
m = tf.constant(22,dtype="int32",name="val2")
with tf.compat.v1.Session() as val:
new_output=val.run(l*m)
print(new_output)
```

In the above program we have used the tf.compat.v1.disable_eager_execution() function and it is used for the difficult programs and it can be used in TensorFlow2.0 instead of tf.session() function.

In the latest version 2.0, the tf.session() has been removed and if you are using the old version of TensorFlow then it works in complex programs.

Now the eager_execution() function works in Tensorflow2.0 instead of session. This function is easy as compared to the session because it implements the operations without creating the graphs.

Here is the implementation of the following given code

Also, check: TensorFlow get shape

## Attributeerror module ‘tensorflow.compat.v2’ has no attribute ‘session’

- In this section, we will discuss the error attributeerror module tensorflow compat v2 has no attribute session.
- In this example we are going to use the
**tf.compat.v2.session()**function. This function is available in TensorFlow**1.x**version and it is used to check the compatibility of Api.

**Example:**

```
import tensorflow as tf
tf.compat.v2.session()
c = tf.constant(43,dtype="int32",name="val1")
d = tf.constant(67,dtype="int32",name="val2")
with tf.compat.v2.Session() as val:
new_output=val.run(c*d)
print(new_output)
```

Here is the execution of the following given code

As you can see in the Screenshot the output displays the attributeerror module TensorFlow compat v2 has no attribute session

**Reason: **In TensorFlow 2.0 version the compact v2 is not available so you have to use the compact v1.disable_eager_execution() function.

Here is the solution to this error

**Code:**

```
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
c = tf.constant(43,dtype="int32",name="val1")
d = tf.constant(67,dtype="int32",name="val2")
with tf.compat.v1.Session() as val:
new_output=val.run(c*d)
print(new_output)
```

In the above example, we have used the **tf.compat.v1.disable_eager_execution() function,** and this function is easy as compared to the session because it implements the operations without creating the graphs. This method is available in Tensorflow **2.0 version**.

You can refer to the below Screenshot

Also, check: Pandas in Python

## Attributeerror module ‘tensorflow.keras.backend’ has no attribute ‘get_session’

- Here we are going to discuss the error
**Attributeerror module ‘tensorflow.keras.backend’ has no attribute****‘get_session’**. - To perform this particular task first we will import the Keras backend library. In Python, it is used to generate the models, and also whenever we are facing low-level computation problems for developing the product the Keras module provides the backend engine.
- Next, we are going to import the TensorFlow library and then create a session for
**tf.keras.backend.get_session**. As you can see in the Screenshot the output displays attributeerror module**‘TensorFlow.Keras.backend’ has no attribute ‘get_session’.**

**Source** **Code:**

```
from keras import backend as K
import tensorflow as tf
tf.keras.backend.get_session()
```

**Reason:** The reason for this error is the session() function is not available in the latest version of TensorFlow.

Solution of this error code

```
from keras import backend as K
import tensorflow as tf
tf.compat.v1.keras.backend.get_session()
```

In this example we have used the tf.compat.v1.keras.backend.get_session() function. This function is available in TensorFlow 2.0 version and it will always return the tf .session which can be used in the backend.

You may also like to read the following tutorials.

- What is NumPy in Python
- Scikit-learn Vs Tensorflow
- What is Matplotlib in Python
- Import error no module named TensorFlow
- TensorFlow Tensor to numpy

In this Python tutorial, we have discussed the error “**module ‘TensorFlow‘ has no attribute ‘session’**“. And we have also covered the following topics:

- attributeerror module ‘tensorflow.keras.backend’ has no attribute ‘get_session’
- attributeerror module ‘tensorflow.compat.v2’ has no attribute ‘session’

Python is one of the most popular languages in the United States of America. I have been working with Python for a long time and I have expertise in working with various libraries on Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… I have experience in working with various clients in countries like United States, Canada, United Kingdom, Australia, New Zealand, etc. Check out my profile.