Explain Codes LogoExplain Codes Logo

Tensorflow 2.0 - AttributeError: module 'tensorflow' has no attribute 'Session'

python
eager-execution
tf-function
migration-guide
Alex KataevbyAlex Kataev·Mar 8, 2025
TLDR

In TensorFlow 2.x, traditional sessions are no more—eager execution is the new norm. For code that's still hanging onto tf.Session(), you kick off compatibility mode like this: switch off eager execution using tf.compat.v1.disable_eager_execution(), then call tf.compat.v1.Session(). Let's get a taste:

import tensorflow.compat.v1 as tf # Sorry eager execution, it's not you, it's them tf.disable_eager_execution() # tf.compat.v1.Session is the nice familar face in the alien world with tf.Session() as sess: # Now it's just like the good old TensorFlow 1.x times

With TensorFlow 2.x, you simply run your tensors and operations directly, who needs a session now?

Differences between TensorFlow 1.x and 2.x

Paradigm shift in TensorFlow 2.0

There's a seismic shift from TensorFlow 1.x to TensorFlow 2.x: deferred execution to eager execution. In TensorFlow 1.x, everything got set up in a graph, then you'd run the whole enchilada in a session. But in TensorFlow 2.x, it's one and done—execute operations immediately, with no waiting around for a session, which is user-friendly to say the least.

Friendly pointer: tf.compat.v1

But hey, who are we to disturb peace? You want some of that TensorFlow 1.x old-school charm in TensorFlow 2.x? tf.compat.v1 has got you covered. It lets you use TensorFlow 1.x features in TensorFlow 2.x like they never left.

Using tf.function for graph goodness in TF 2.x

But what about those operations that still need graph execution? Don't weep just yet. TensorFlow 2.x has your back with tf.function(). It allows you to put your functions in a pretty gift wrapper that lets TensorFlow create optimised graphs, perfect for your performance needs.

Updating warriors win the war

As in life, so in TensorFlow 2.x—everything keeps evolving. It's crucial to walk hand in hand with the official TensorFlow documentation and keep your ears open to community updates. Stick to your best practices and swerve around the deprecated paths to avoid errors like our friend AttributeError.

Migrating to the TensorFlow 2.x world

TensorFlow incompatibility is not your destiny

Drawing TensorFlow 1.x functionality into TensorFlow 2.0 might feel like a life jacket, but it won't make you a great swimmer. Your goal should be to refactor your code to sink or swim in TensorFlow 2.x's waters. This means embracing eager execution and taking tf.function() to your heart, getting away from the tiresome session-based marriage.

Cheat sheet for clean migration

To make your migration from TensorFlow 1.x to 2.x as smooth as baby's skin, check out the migration guide. This gives you step by step transformation of your code to make it TensorFlow 2.x friendly.

All debuggers need a debugger's best friend - tf.print

Say a warm goodbye to Python print statements within your graph-related code. Instead, give a hearty welcome to tf.print(), which blends with the TensorFlow execution environment like milk with coffee, providing a better debugging experience.

Syntax surgery in TensorFlow 2.x

TensorFlow 2.x has seen functions get a new look, new names and even new behaviours. Keeping your syntax up-to-date is crucial to avoid stepping on errors. Consult the TF 1:1 Symbols Map regularly to keep your codebase tidy.