'ModuleNotFoundError: No module named 'keras.backend.tensorflow_backend'

I am trying to run the code

import keras

And I am getting this stack trace. I have tried reinstalling keras and tensorflow but nothing in working. Here is the stack trace.

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-10-88d96843a926> in <module>
----> 1 import keras

~\Anaconda3\lib\site-packages\keras\__init__.py in <module>
      1 from __future__ import absolute_import
      2 
----> 3 from . import utils
      4 from . import activations
      5 from . import applications

~\Anaconda3\lib\site-packages\keras\utils\__init__.py in <module>
      4 from . import data_utils
      5 from . import io_utils
----> 6 from . import conv_utils
      7 from . import losses_utils
      8 from . import metrics_utils

~\Anaconda3\lib\site-packages\keras\utils\conv_utils.py in <module>
      7 from six.moves import range
      8 import numpy as np
----> 9 from .. import backend as K
     10 
     11 

~\Anaconda3\lib\site-packages\keras\backend\__init__.py in <module>
----> 1 from .load_backend import epsilon
      2 from .load_backend import set_epsilon
      3 from .load_backend import floatx
      4 from .load_backend import set_floatx
      5 from .load_backend import cast_to_floatx

~\Anaconda3\lib\site-packages\keras\backend\load_backend.py in <module>
     88 elif _BACKEND == 'tensorflow':
     89     sys.stderr.write('Using TensorFlow backend.\n')
---> 90     from .tensorflow_backend import *
     91 else:
     92     # Try and load external backend.

ModuleNotFoundError: No module named 'keras.backend.tensorflow_backend'


Solution 1:[1]

Try:

pip install tensorflow==2.2.0

and then

pip install Keras==2.2.0

This worked for me with Python 3.7.

Solution 2:[2]

instead of use something like

    from keras.backend.tensorflow_backend import set_session

Try to use it like

    from keras.backend import set_session

Solution 3:[3]

In Tensorflow 2.0.0+ versions you should just put "compat.v1" after tf and dont use "tensorflow_backend" name. Like this:

tf.keras.backend.tensorflow_backend.set_session() -> tf.compat.v1.keras.backend.set_session()

Solution 4:[4]

I tried to use anaconda or pip to install tensorflow and keras, and each method met the same problem.

At last I found the problem is because the version of tensorflow or keras. When I install tensorflow==2.2 and keras==2.4.3(latest), no matter which tools I used I will meet this problem.When I install tensorflow==1.14 and keras==2.2, the code works well.

My python version is 3.5.2 under ubuntu 16.04

Solution 5:[5]

Just install tensorflow 2.1.0 or 2.2.0 It already has Keras inside. Dont mix using pip and conda. Carry on with what you have started.

pip install tensorflow==2.2.0

or,

conda install tensorflow==2.2.0

Solution 6:[6]

Uninstall Keras and reinstall the version 2.2.0 in your system, it will definately work with Tensorflow 2.2. Then you won't have to downgrade you tensorflow ie. less pain of changing codes ;)

pip uninstall keras
pip install Keras==2.2.0

For my case, I had Python 3.7(latest bug fix)

Solution 7:[7]

for tensorflow==2.4.1 this works:

from tensorflow.python.keras.backend import set_session 

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 Adrian Mole
Solution 2 Ivan Opitz
Solution 3 Ege
Solution 4 Timothy Wu
Solution 5
Solution 6 H Soora
Solution 7 Ali Maroosi