Home > Mobile >  ModuleNotFoundError: No module named 'keras.api' error
ModuleNotFoundError: No module named 'keras.api' error

Time:03-10

I am trying to run a ".py" file on windows 10 with tensorflow version 2.8.0 and keras version 2.3.4, where I am calling libraries as follows

from tensorflow import keras 
from keras.models import load_model

However, I am getting an error message "ModuleNotFoundError: No module named 'keras.api'" as shown in the error log below.

DeprecationWarning: The distutils package is deprecated and slated for removal in Python 3.12. Use setuptools or check PEP 632 for potential alternatives
  from distutils.log import debug
Using TensorFlow backend.
Traceback (most recent call last):
  File "C:\Users\saniy\OneDrive\Derma-Project\app1.py", line 5, in <module>
    from keras.models import load_model
  File "C:\Python310\lib\site-packages\keras\__init__.py", line 3, in <module>
    from . import utils
  File "C:\Python310\lib\site-packages\keras\utils\__init__.py", line 26, in <module>
    from .vis_utils import model_to_dot
  File "C:\Python310\lib\site-packages\keras\utils\vis_utils.py", line 7, in <module>
    from ..models import Model
  File "C:\Python310\lib\site-packages\keras\models.py", line 12, in <module>
    from .engine.training import Model
  File "C:\Python310\lib\site-packages\keras\engine\__init__.py", line 8, in <module>
    from .training import Model
  File "C:\Python310\lib\site-packages\keras\engine\training.py", line 14, in <module>
    from . import training_utils
  File "C:\Python310\lib\site-packages\keras\engine\training_utils.py", line 17, in <module>
    from .. import metrics as metrics_module
  File "C:\Python310\lib\site-packages\keras\metrics.py", line 1850, in <module>
    BaseMeanIoU = tf.keras.metrics.MeanIoU
  File "C:\Python310\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 58, in __getattr__
    module = self._load()
  File "C:\Python310\lib\site-packages\tensorflow\python\util\lazy_loader.py", line 41, in _load
    module = importlib.import_module(self.__name__)
  File "C:\Python310\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
ModuleNotFoundError: No module named 'keras.api'

I am wondering if you can help in this regard.

CodePudding user response:

I am not sure how keras 2.3.4 is installed but minimum version of keras for tf 2.8 is 2.8; Just upgrade your keras version

pip install keras==2.8

CodePudding user response:

Since version 2 of Tensorflow (TF), the Keras package comes installed alongside. Meaning that if you make pip install tensorflow, will install the latest TF version (2.8) and Keras 2.8. As the other answer suggests, I would guess you have some old Keras version installed on your computer.

My advise would be to create a fresh virtual environment,

python3 -m venv ENV_DIR

and then freshly install all the requirements that your .py file needs from scratch. This will hopefully solve your issue and it is also best practice not to mess with different Python packages versions throughout your projects.

  • Related