Tensorflow 2.0 - AttributeError module 'tensorflow' has no attribute 'Session'
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
TensorFlow 2.0 introduced significant changes in its API, reflecting a shift towards a more user-friendly and intuitive interface. One common issue users encounter when transitioning from TensorFlow 1.x to TensorFlow 2.0 is the AttributeError: module 'tensorflow' has no attribute 'Session'. This article provides an in-depth explanation of this error, examines its causes, and offers solutions for developers moving to the newer version.
Understanding the TensorFlow Session AttributeError
The error message AttributeError: module 'tensorflow' has no attribute 'Session' arises from the architectural shift in TensorFlow 2.0, where eager execution replaces session-based execution as the default mode. Here's a breakdown of what this means:
Eager Execution vs. Session-Based Execution
- Session-Based Execution (TensorFlow 1.x):
- In TensorFlow 1.x, building a computational graph and executing it were two separate steps: you first defined the computational graph in a "declaration" phase and then executed it in a "session."
- The
Session()object managed the execution of this graph.
- Eager Execution (TensorFlow 2.0):
- Eager execution allows operations to be evaluated immediately without building graphs. This is similar to Python's built-in data structures like lists and numpy arrays.
- This makes debugging and experimentation easier, as you don't need to explicitly manage sessions.
Addressing the AttributeError
- Adopting Eager Execution:Most operations, previously enclosed in a session, can now be run directly as TensorFlow objects and functions in eager execution mode.
- Using
tf.function:When performance becomes a concern, you can convert Python functions that use TensorFlow operations into TensorFlow graphs usingtf.function.
- Migrating Legacy Code:For projects with substantial amounts of 1.x code, you can use the
tensorflow.compat.v1module, which provides a compatibility layer.
Summary Table
| TensorFlow Version | Execution Mode | Execution Example |
| 1.x | Session-Based | sess.run() with tf.Session() object |
| 2.0 | Eager Execution | Run immediately upon definition using print() |
2.0 with tf.function | Graph Computation | Wrap function with @tf.function for graph execution |
| Compatibility Mode | Use Legacy APIs | tensorflow.compat.v1 with sess.run() as fallback |
Moving Forward with TensorFlow 2.0
Understanding the differences in execution paradigms is crucial when working with TensorFlow 2.0. Here are some strategies for a smoother transition:
- Learn New APIs: Familiarize yourself with new high-level APIs that leverage eager execution, such as Keras.
- Profiling and Performance: Use TensorBoard and other profiling tools provided by TensorFlow to analyze model performance.
- Experimentation and Prototyping: Leverage eager execution for rapid prototyping and iterative experimentation.
By embracing these changes, developers can harness the full power and simplicity of TensorFlow 2.0 while leveraging new features and improved usability.

