Recently, while working on a deep learning project using TensorFlow and Keras, I encountered an error that’s quite common among developers transitioning from TensorFlow 1.x to 2.x: AttributeError: module ‘keras.backend’ has no attribute ‘get_session’.
This error occurs when your code is trying to access the get_session() method that existed in TensorFlow 1.x but has been removed in TensorFlow 2.x.
In this article, I’ll explain why this error happens and provide multiple solutions to resolve it. Let’s get in and tackle this frustrating issue once and for all!
What Causes This Error?
Before jumping into the solutions, it’s important to understand why this error occurs in the first place.
The get_session() function was commonly used in TensorFlow 1.x to retrieve the current TensorFlow session. However, with TensorFlow 2.x’s eager execution by default, sessions are no longer the primary way of executing operations.
When you try to use K.get_session() in TensorFlow 2.x code, Python raises the error because this method simply doesn’t exist anymore in the new version.
Method 1: Use TensorFlow Compatibility Module
One of the easiest ways to fix this error is to use TensorFlow’s compatibility module, which provides access to TensorFlow 1.x APIs.
import tensorflow as tf
# Disable eager execution to use TF 1.x-style session
tf.compat.v1.disable_eager_execution()
# Manually create a session
session = tf.compat.v1.Session()
# Define computation
a = tf.constant(5)
b = tf.constant(7)
c = a + b
# Run and print the result using the session
result = session.run(c)
print("Sum of 5 and 7 is:", result)Output:
Sum of 5 and 7 is: 12You can refer to the screenshot below to see the output.

This approach allows you to use the old API without downgrading your TensorFlow version. It’s particularly useful when you’re migrating legacy code and don’t want to rewrite everything at once.
Read TensorFlow Convolution Neural Network
Method 2: Import from tensorflow.python.keras.backend
Another solution that works well is to import the function directly from the Python module:
import tensorflow as tf
from tensorflow.python.keras.backend import get_session
# Disable eager execution to use get_session()
tf.compat.v1.disable_eager_execution()
# Create constants and an operation
a = tf.constant(3)
b = tf.constant(4)
c = a * b
# Get the session using the imported method
session = get_session()
# Run the operation
result = session.run(c)
print("Product of 3 and 4 is:", result)Output:
Product of 3 and 4 is: 12You can refer to the screenshot below to see the output.

This method has been confirmed to work by several developers who faced the same issue.
Check out AttributeError: Module ‘tensorflow’ has no attribute ‘placeholder’
Method 3: Rewrite Your Code to Use TensorFlow 2.x APIs
The most forward-looking approach is to update your code to use TensorFlow 2.x native APIs instead of relying on the old session-based approach.
import tensorflow as tf
# TensorFlow 2.x runs in eager mode by default (no sessions needed)
a = tf.constant(8)
b = tf.constant(5)
# Operation executes immediately
result = tf.add(a, b)
print("Sum of 8 and 5 is:", result.numpy())Output:
Sum of 8 and 5 is: 13You can refer to the screenshot below to see the output.

This method requires more code changes but ensures your code is future-proof and takes advantage of the improvements in TensorFlow 2.x.
Read AttributeError: Module ‘tensorflow’ has no attribute ‘sparse_tensor_to_dense’
Method 4: Use TensorFlow 1.x Behavior in TensorFlow 2.x
If you have a large codebase that heavily relies on TensorFlow 1.x patterns and you’re not ready to migrate everything, you can enable TensorFlow 1.x behavior:
import tensorflow as tf
tf.compat.v1.disable_eager_execution()
# Now you can use the v1 API
session = tf.compat.v1.keras.backend.get_session()This approach lets you run TensorFlow 1.x code with minimal changes, though it’s recommended as a temporary solution while you migrate your code.
Method 5: Use a Specific Version of TensorFlow
If none of the above solutions work for your specific use case, you might consider using an earlier version of TensorFlow where get_session() is still available:
# Uninstall current TensorFlow
pip uninstall tensorflow
# Install TensorFlow 1.x
pip install tensorflow==1.15.0However, I don’t recommend this approach for new projects as TensorFlow 1.x is no longer receiving feature updates, and you’ll miss out on performance improvements and new features.
Check out Module ‘tensorflow’ has no attribute ‘sparse_placeholder’
Real-World Example: Object Detection Model
Let’s look at a practical example. Say you’re working on an object detection system for a U.S. retail store to identify products on shelves:
# Original code causing the error
from imageai.Detection import ObjectDetection
import keras.backend as K
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolo.h5")
detector.loadModel()
# Get session to perform operations
session = K.get_session() # This line causes the errorTo fix this, you can use:
from imageai.Detection import ObjectDetection
import tensorflow as tf
detector = ObjectDetection()
detector.setModelTypeAsYOLOv3()
detector.setModelPath("yolo.h5")
detector.loadModel()
# If you absolutely need a session:
session = tf.compat.v1.keras.backend.get_session()This approach works fine with TensorFlow 2.2 without requiring a downgrade.
Prevention Tips
To avoid this error in future projects:
- Always check the TensorFlow version compatibility when using libraries that depend on TensorFlow
- Use version pinning in your requirements.txt or environment.yml files
- Consider using Docker to ensure consistent environments
- Stay updated with TensorFlow’s migration guides when upgrading versions
I hope you found this guide helpful in resolving the AttributeError: module 'keras.backend' has no attribute 'get_session' error. Understanding the differences between TensorFlow 1.x and 2.x is crucial for smooth development of deep learning applications.
If you’re working with TensorFlow, I recommend embracing the new APIs rather than relying on compatibility layers for new projects. The eager execution model in TensorFlow 2.x provides a more intuitive and Pythonic experience that will make your code cleaner and easier to debug.
Other Python articles you may also like:
- Module ‘tensorflow’ has no attribute ‘optimizers’
- Module ‘tensorflow’ has no attribute ‘log’
- AttributeError: Module ‘tensorflow’ has no attribute ‘global_variables_initializer’

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.