# tf.executing_eagerly

<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tensorflow/blob/v2.13.1/tensorflow/python/eager/context.py#L2250-L2308) |

Checks whether the current thread has eager execution enabled.

    tf.executing_eagerly()

Eager execution is enabled by default and this API returns `True`
in most of cases. However, this API might return `False` in the following use
cases.

- Executing inside [`tf.function`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/function), unless under [`tf.init_scope`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/init_scope) or [`tf.config.run_functions_eagerly(True)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/config/run_functions_eagerly) is previously called.
- Executing inside a transformation function for `tf.dataset`.
- [`tf.compat.v1.disable_eager_execution()`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/compat/v1/disable_eager_execution) is called.

#### General case:

    print(tf.executing_eagerly())
    True

Inside [`tf.function`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/function):

    @tf.function
    def fn():
      with tf.init_scope():
        print(tf.executing_eagerly())
      print(tf.executing_eagerly())
    fn()
    True
    False

Inside [`tf.function`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/function) after [`tf.config.run_functions_eagerly(True)`](https://www.tensorflow.org/versions/r2.13/api_docs/python/tf/config/run_functions_eagerly) is called:

    tf.config.run_functions_eagerly(True)
    @tf.function
    def fn():
      with tf.init_scope():
        print(tf.executing_eagerly())
      print(tf.executing_eagerly())
    fn()
    True
    True
    tf.config.run_functions_eagerly(False)

Inside a transformation function for `tf.dataset`:

    def data_fn(x):
      print(tf.executing_eagerly())
      return x
    dataset = tf.data.Dataset.range(100)
    dataset = dataset.map(data_fn)
    False

<br />

<br />

<br />

| ## Returns ||
|---|---|
| `True` if the current thread has eager execution enabled. ||

<br />