'ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' [start point : from keras.layers.core import Activation]

I am getting the following error :

Traceback (most recent call last):
    File "Estimate parameters with lstm.py", line 13, in <module>
         from keras.layers.core import Activation
    File "/home/zeus/my_env/lib/python3.8/site-packages/keras/__init__.py", line 25, in <module>
         from keras import models
    File "/home/zeus/my_env/lib/python3.8/site-packages/keras/models.py", line 19, in <module>
         from keras import backend
   File "/home/zeus/my_env/lib/python3.8/site-packages/keras/backend.py", line 36, in <module>
         from tensorflow.python.eager.context import get_config
   ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' 

I have tried from tensorflow import keras instead of import keras, but the error is still there.



Solution 1:[1]

There are many causes that this happens.

Solution 1: upgrade tensorflow

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

Solution 2: Change import Methods

Instead of this

import keras

Use this one

from tensorflow import keras

if you are using image, then instated of this

from keras.preprocessing import image

use this one

from tensorflow.keras.preprocessing import image

Solution 2:[2]

This is TensorFlow and keras version mismatch error.

Importing keras from TensorFlow or upgrading TensorFlow will solve this issue.(@paraphrased by Ali Aref)

OR

Make sure you are using same versions of TensorFlow and keras or atleast latest TensorFlow version 2.7 to execute your code which may fix this issue.

Check TensorFlow and keras version using below code:

import tensorflow
print(tf.__version__)
import keras
print(keras.__version__)

And try installing the same keras version as existing installed TensorFlow:

!pip install keras==<enter installed tensorflow version>

Run your code again and let us know if issue still persists.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ali Aref
Solution 2