Error AttributeError: module 'tensorflow.compat.v2.__internal__' has no attribute register_load_context_function


I have just learned deep learning models. So I tried to install the Tensorflow as per the instructions in the link:- Install TensorFlow 2

After installing, when I am doing import keras. System is giving me following errors :-1:

import keras
Traceback (most recent call last):
File “”, line 1, in
File “C:\Users\ragha\AppData\Local\Programs\Python\Python39\lib\site-packages\”, line 21, in
from keras import models
File “C:\Users\ragha\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\”, line 18, in
from keras.engine.functional import Functional
File “C:\Users\ragha\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\”, line 34, in
from keras.engine import training as training_lib
File “C:\Users\ragha\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\engine\”, line 45, in
from keras.saving.legacy import save
File “C:\Users\ragha\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\saving\legacy\”, line 24, in
from keras.saving.legacy.saved_model import load_context
File “C:\Users\ragha\AppData\Local\Programs\Python\Python39\lib\site-packages\keras\saving\legacy\saved_model\”, line 68, in
AttributeError: module ‘tensorflow.compat.v2.internal’ has no attribute ‘register_load_context_function’

So I need help in resolving this error. So that I can work on Tensor Flow models.

can you share you code here? and which verison of keras and tensorflow you are using

To use Keras, you will need to have the TensorFlow package installed. See detailed instructions.

Once TensorFlow is installed, just import Keras via:

from tensorflow import keras

Please try as suggested above and let us know if the problem continues? Thank you

Thanks for your help.

I have removed the tf conda environment completely and then installed the environment again.

I have then again installed all the libraries as mentioned in the detailed instructions link.

But after installing, I was again getting some different errors of module not found for keras. So I then installed tensorflow in the main environment too (Without conda tf environment). Post this I was able to run the tensorflow code

I found that problem is from Keras packages
so I execute these commands

pip uninstall keras
pip install tensorflow