Home » ImportError: cannot import name ‘get_config’ from ‘tensorflow.python.eager.context’

ImportError: cannot import name ‘get_config’ from ‘tensorflow.python.eager.context’

To solve ImportError: cannot import name ‘get_config’ from ‘tensorflow.python.eager.context’ error follow below methods.


ImportError                               Traceback (most recent call last)
<ipython-input-1-589c442233c5> in <module>()
      7 import tensorflow as tf
      8 from tensorflow.keras import datasets, layers, models
----> 9 from keras.preprocessing import image
     10 from keras_preprocessing.image import ImageDataGenerator #check underscore or not
     11 from tensorflow.keras.preprocessing import image_dataset_from_directory

2 frames
/usr/local/lib/python3.7/dist-packages/keras/backend.py in <module>()
     35 from tensorflow.python.distribute import distribute_coordinator as dc
     36 from tensorflow.python.distribute import distribute_coordinator_context as dc_context
---> 37 from tensorflow.python.eager.context import get_config
     38 from tensorflow.python.framework import config
     39 from keras import backend_config

ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (/usr/local/lib/python3.7/dist-packages/tensorflow_core/python/eager/context.py)

How to solve ImportError: cannot import name ‘get_config’ from ‘tensorflow.python.eager.context’ ?

This problem was resolved by just upgrading TensorFlow and TensorFlow GPU. Simply run the following command to upgrade TensorFlow and TensorFlow GPU. simply run in your console

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu

Alternative to Solve ImportError: cannot import name ‘get_config’ from ‘tensorflow.python.eager.context’

Instead of directly importing keras as shown below

import keras


from tensorflow import keras 

If you are using Image then use instead of 

from keras.preprocessing import image

Replace above code by below line

from tensorflow.keras.preprocessing import image

Hope the above solution works.

Also read :

pip install failing on python2
get-pip.py returns SyntaxError: invalid syntax