AttributeError __enter__ from with tf.Session as sess
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
This error usually means Python tried to use something in a with statement that is not an actual context-manager instance. In old TensorFlow 1 code, the most common cause is writing with tf.Session as sess: instead of with tf.Session() as sess:. In newer TensorFlow 2 code, the deeper issue is often that you should not be using sessions at all.
Why __enter__ Matters
Python's with statement only works with objects that implement the context manager protocol. That protocol includes:
- '
__enter__' - '
__exit__'
For example:
The object returned by open(...) supports that protocol, so with works.
If you use a class or object that does not provide __enter__, Python raises an attribute error involving __enter__.
A Quick Sanity Check
When this error is unclear, inspect what you are passing into the with block. If it is a class object instead of an instance, that is usually the bug.
That kind of check makes the failure obvious and is often faster than reading the full stack trace first.
The Most Common TensorFlow 1 Mistake
This is wrong:
tf.Session there is the class itself, not an instance of the class. You forgot the parentheses.
The TensorFlow 1 style you actually wanted was:
That creates a session object, and that object supports use in a with block.
TensorFlow 2 Changes the Story
In TensorFlow 2, eager execution is the default, so most code should not use Session at all.
Modern TensorFlow 2 code usually looks like this:
If you are seeing tf.Session examples in old tutorials, you are probably reading TensorFlow 1 material.
In TensorFlow 2 compatibility mode, the session form becomes:
That is only for legacy code paths or migration situations.
How to Decide Which Fix You Need
Use this rule:
- if you are on TensorFlow 1 style code, add the missing parentheses
- if you are on TensorFlow 2 normal eager code, remove the session entirely
- if you are migrating legacy code in TensorFlow 2, use
tf.compat.v1.Session()
That distinction matters because adding parentheses may fix the syntax error while still leaving you with outdated TensorFlow architecture.
Example of the Migration Path
Old TensorFlow 1 style:
Equivalent TensorFlow 2 eager style:
If you are starting new code, prefer the second form.
Common Pitfalls
- Writing
with tf.Session as sess:and forgetting the parentheses. - Copying TensorFlow 1 examples into TensorFlow 2 eager code without realizing the API model changed.
- Using
tf.Session()in new TensorFlow 2 code when a direct tensor computation would be simpler. - Assuming every object in a
withblock automatically supports context management. - Fixing the syntax but not noticing the codebase is still using legacy TensorFlow patterns unnecessarily.
Summary
- The immediate cause of this error is usually using
tf.Sessioninstead oftf.Session(). - A
withblock needs an actual context-manager instance, not a class object. - In TensorFlow 2, most code should avoid sessions entirely and use eager execution.
- For legacy migration, use
tf.compat.v1.Session()if you truly need session-based execution. - Fix the syntax first, then decide whether the whole session-based approach is still appropriate.

