1

My system information :

Windows version :  11
Python version  :  3.10.7
Tensorflow      :  2.11.0
pip             :  22.3.1

I have checked the previous questions which are similar to mine but they didn't help.

I prefer to use virtual env over Conda thats why I am not using tensorflow through Conda.

When I run the below program in Jupyter lab its giving error.

import sys
sys.version

'3.10.7 (tags/v3.10.7:6cc6b13, Sep 5 2022, 14:08:36) [MSC v.1933 64 bit (AMD64)]'

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow.keras as keras   

ModuleNotFoundError                       Traceback (most recent call last)
Input In [2], in <cell line: 5>()
      3 import matplotlib.pyplot as plt
      4 import tensorflow as tf
----> 5 import tensorflow.keras as keras

ModuleNotFoundError: No module named 'tensorflow.keras'

How to fix ModuleNotFoundError: No module named 'tensorflow.keras' ?

Udesh
  • 2,415
  • 2
  • 22
  • 32
  • If you comment out the Keras line and instead check the version of tensorflow, what do you get? Is it what you expect? – Mike L Nov 26 '22 at 09:28
  • How to check tensorflow version? – Udesh Nov 26 '22 at 09:36
  • Try printing out tf.__version__ – Mike L Nov 26 '22 at 09:39
  • Executing `tf.__version__` gives `AttributeError: module 'tensorflow' has no attribute '__version__'` – Udesh Nov 26 '22 at 09:39
  • I suggest googling these problems until you can print out that version and verify it's correct – Mike L Nov 26 '22 at 09:48
  • `pip freeze` gives tensorflow version is tensorflow==2.11.0 – Udesh Nov 26 '22 at 09:49
  • This error sounds like the tensorflow you're using at runtime might be different than the one you see at your command line. I could be wrong, so knowing that version from inside Python is useful to rule out this source of the issue – Mike L Nov 26 '22 at 09:53
  • `pip show tensorflow` statement in the **Jupyter notebook** displayed the tensorflow `Version: 2.11.0` – Udesh Nov 26 '22 at 09:59
  • I don't have any directory named tensorflow – Udesh Nov 26 '22 at 11:42
  • after importing tensorflow, do print(tensorflow) and report the result here. – Dr. Snoopy Nov 26 '22 at 14:03
  • I think your tensorflow installation is somehow incomplete or corrupted, because the print should show something like: /.local/lib/python3.7/site-packages/tensorflow/__init__.py'> or similar path. – Dr. Snoopy Nov 26 '22 at 20:00
  • Actually, when you import tensorflow, do you get errors or strange messages? – Dr. Snoopy Nov 26 '22 at 20:02
  • No while importing tensorflow into my project doesn't give any **error**. @Dr Snoppy I think you are correct `the tensorflow installation is somehow incomplete or corrupted`. I uninstalled the current latest version of tensorflow and installed previous version of it and this has fixed the issue. Now when I `print(tensorflow)` it is displaying the path of tensorflow and I can also `import tensorflow.keras`. – Udesh Nov 27 '22 at 03:43

2 Answers2

0

Issue will resolve easily by doing the following steps

  1. First you have to check whether you had installed tensorflow in system or not if yes then it will work in jupyter notebook.

  2. But after installing in system tensorflow it shows this error same then uninstall python version and download 3.9 version and after that again install tensorflow in cmd and install in system not in through virtual environment so jupyter note book you can download and it will be work after importing tensorflow

  3. Delete anaconda its useful but it is upto you so this was the simple steps you should follow and you'll see that the issue has resolved.

Hope, this will help you

Udesh
  • 2,415
  • 2
  • 22
  • 32
  • Thanks @Krish for your answer but don't want to use older version of python as of now seems tensorflow doesn't full support `Python 3.11` buy it does support `Python 3.10`. I would like to use tensorflow on `Python3.10`. – Udesh Nov 26 '22 at 10:04
0

This means that to be able to use the Keras library, you will have to install TensorFlow first and when you import the Keras library !pip install TensorFlow