ModuleNotFoundError: No module named 'tensorflow.keras'

I have both
tensorflow 2.15.0
keras 2.15.0

I really don’t have an Idea to fix this, I have tried to reinstall everything still same result.

NEED HELP PLEASE, THANK YOU ALL!!!

this is the code:

import tensorflow as tf

def test_tensorflow():
    # Check TensorFlow version
    print("TensorFlow version:", tf.__version__)

    # Create a simple TensorFlow constant
    hello = tf.constant("Hello, TensorFlow!")

    # Start a TensorFlow session
    with tf.compat.v1.Session() as session:
        # Run the session to evaluate the 'hello' tensor
        result = session.run(hello)

    # Print the result
    print("TensorFlow says:", result.decode("utf-8"))

if __name__ == "__main__":
    test_tensorflow()

this is the error:

Traceback (most recent call last):
  File "d:\school\comscie\emotion\mema.py", line 1, in <module>
    import tensorflow as tf
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tensorflow\__init__.py", line 48, in <module>
    from tensorflow._api.v2 import __internal__
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tensorflow\_api\v2\__internal__\__init__.py", line 8, in <module>
    from tensorflow._api.v2.__internal__ import autograph
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tensorflow\_api\v2\__internal__\autograph\__init__.py", line 8, in <module>
    from tensorflow.python.autograph.core.ag_ctx import control_status_ctx # line: 34
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tensorflow\python\autograph\core\ag_ctx.py", line 21, in <module>
    from tensorflow.python.autograph.utils import ag_logging
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tensorflow\python\autograph\utils\__init__.py", line 17, in <module>
    from tensorflow.python.autograph.utils.context_managers import control_dependency_on_returns
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tensorflow\python\autograph\utils\context_managers.py", line 19, in <module>
    from tensorflow.python.framework import ops
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\tensorflow\python\framework\ops.py", line 26, in <module>
    from absl import app
  File "C:\Users\ADMIN\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\absl\app.py", line 31, in <module>        
    import pdb
  File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\pdb.py", line 77, in <module>
    import code
  File "d:\school\comscie\emotion\code.py", line 8, in <module>
    from tensorflow.keras.preprocessing import image
ModuleNotFoundError: No module named 'tensorflow.keras'

Hi @Samurai_Warior, The code you mentioned above does not contain any import of kears.

The error was triggered from this import statement. Instead of importing keras from tensorflow could you please try by importing keras directly using import keras. Thank you.

1 Like

@Samurai_Warior You are on TensorFlow 2.X but in your code, you are using the older ‘tf.compat.v1.Session()’ syntax, which suggests you might be using TensorFlow 1.x. You may want to upgrade your code to use eager execution, that way you don’t have to use the ‘tf.compat.v1.Session()’ syntax.

I hope you find this helpful