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.
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
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
I'm following the TenslorFlow TFlite basic text classification tutorial, but the third import line throws an error:
from tensorflow_examples.lite.model_maker.core.data_util import image_dataloader
ModuleNotFoundError: No module named 'official.nlp.bert'
I installed TensorFlow with pip install tensorflow and the tflite model maker with pip install -q tflite-model-maker.
I've also installed all the official models with pip install tf-models-official.
I can see the BERT model on the official models GitHub as well, so it should be in there.
How can I install the bert model?
Full error:
Traceback (most recent call last):
File "...\Downloads\tf_setup.py", line 6, in <module>
from tflite_model_maker import model_spec
File "...\AppData\Local\Programs\Python\Python310\lib\site-packages\tflite_model_maker\__init__.py", line 16, in <module>
from tensorflow_examples.lite.model_maker.core.data_util import image_dataloader
File "...\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow_examples\lite\model_maker\__init__.py", line 17, in <module>
from tensorflow_examples.lite.model_maker.core.data_util import text_dataloader
File "...\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow_examples\lite\model_maker\core\data_util\text_dataloader.py", line 31, in <module>
from tensorflow_examples.lite.model_maker.core.task import model_spec as ms
File "...\AppData\Local\Programs\Python\Python310\lib\site-packages\tensorflow_examples\lite\model_maker\core\task\model_spec.py", line 37, in <module>
from official.nlp.bert import configs as bert_configs
ModuleNotFoundError: No module named 'official.nlp.bert'
What is your Python version? I encountered the exact same error while I was using Python 3.10. My tflite-model-maker was 0.1.x version despite I tried to install the latest one which is 0.4.0. I wanted to update it but I got more errors about "No matching distribution" for tflite-model-maker and tflite-support. After I installed and changed my Python interpreter (I'm using PyCharm) to Python 3.9.13 and reinstall tflite-model-maker, the error is gone.
I try to test my installation of tensorflow model garden and execute the following command in my PowerShell:
python object_detection/builders/model_builder_tf2_test.py
Thereby, I get the following error message:
Traceback (most recent call last):
File "...\Documents\TensorFlow\models\research\object_detection\builders\model_builder_tf2_test.py", line 21, in <module>
import tensorflow.compat.v1 as tf
File "...\Anaconda3\lib\site-packages\tensorflow\__init__.py", line 37, in <module>
from tensorflow.python.tools import module_util as _module_util
File "...\Anaconda3\lib\site-packages\tensorflow\python\__init__.py", line 37, in <module>
from tensorflow.python.eager import context
File "...\Anaconda3\lib\site-packages\tensorflow\python\eager\context.py", line 29, in <module>
from tensorflow.core.framework import function_pb2
File "...\Anaconda3\lib\site-packages\tensorflow\core\framework\function_pb2.py", line 16, in <module>
from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
File "...\Anaconda3\lib\site-packages\tensorflow\core\framework\attr_value_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
File "...\Anaconda3\lib\site-packages\tensorflow\core\framework\tensor_pb2.py", line 16, in <module>
from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
File "...\Anaconda3\lib\site-packages\tensorflow\core\framework\resource_handle_pb2.py", line 16, in <module>
from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
File "...\Anaconda3\lib\site-packages\tensorflow\core\framework\tensor_shape_pb2.py", line 36, in <module>
_descriptor.FieldDescriptor(
File "...\Anaconda3\lib\site-packages\google\protobuf\descriptor.py", line 560, in __new__
_message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
In my opinion, I need to install a later version than 3.19.0. of protoc, because the cal from a _pb2.py - file. As a result I looked for the version of protoc with pip show protobuf and get the following result:
Name: protobuf
Version: 4.21.1
Summary:
Home-page: https://developers.google.com/protocol-buffers/
Author: protobuf#googlegroups.com
Author-email: protobuf#googlegroups.com
License: 3-Clause BSD License
Location: ...\anaconda3\lib\site-packages
Requires:
Required-by: tensorflow, tensorflow-metadata, tensorflow-hub, tensorflow-datasets, tensorboard, proto-plus, googleapis-common-protos, google-api-core, apache-beam
So, there is installed a later version than 3.19.0 but it doesn't work.
Why does it not work and what can I do that it works?
For your information: I used the tutorial of tensorflow on this site.
Google introduced a new breaking change in protobuf-4.21.0. Anything later than that will not work. You need to install protobuf 3.20.x or earlier (like version 3.20.3.)
Alternately, you could set the environment variable that it recommends. I have found that it is convenient to put it into your __init__.py file like:
import os
# Google introduced an incompatibility into protobuf-4.21.0
# that is not backwards compatible with many libraries.
# Once those libraries have updated to rebuild their _pb2.py files,
# this can be removed.
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
The other solution would be to upgrade Tensorflow if they have a new version out that rebuilds with the latest protobuf.
I installed librosa, and now im trying to import it
import librosa
This throws me this error for some reason
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\Asish nayak\AppData\Local\Programs\Python\Python38-32\lib\site-packages\librosa\__init__.py", line 12, in <module>
from . import core
File "C:\Users\Asish nayak\AppData\Local\Programs\Python\Python38-32\lib\site-packages\librosa\core\__init__.py", line 125, in <module>
from .time_frequency import * # pylint: disable=wildcard-import
File "C:\Users\Asish nayak\AppData\Local\Programs\Python\Python38-32\lib\site-packages\librosa\core\time_frequency.py", line 11, in <module>
from ..util.exceptions import ParameterError
File "C:\Users\Asish nayak\AppData\Local\Programs\Python\Python38-32\lib\site-packages\librosa\util\__init__.py", line 77, in <module>
from .utils import * # pylint: disable=wildcard-import
File "C:\Users\Asish nayak\AppData\Local\Programs\Python\Python38-32\lib\site-packages\librosa\util\utils.py", line 15, in <module>
from .decorators import deprecated
File "C:\Users\Asish nayak\AppData\Local\Programs\Python\Python38-32\lib\site-packages\librosa\util\decorators.py", line 9, in <module>
from numba.decorators import jit as optional_jit
ModuleNotFoundError: No module named 'numba.decorators'
Why am i getting this error even after installing the library?
You have to install numbra version 0.48 because of a bug, run
pip install numba==0.48
Librosa Issue on Github
You need to use any numba version before 0.8.0
However, the most recent librosa is not compatible with any numba below 0.48.
At least, that's what I've found from using it on Windows. My solution was to use google colaboratory as it didn't give me any error when importing librosa.
I've got the exact error. Solved it with
pip uninstall --yes librosa
pip install librosa --force-reinstall
Your python version should be 3.8.
Since my CPU doesn't support AVX instructions and I want to install a newer version of Tensorflow (>= 2.0), I installed a tensorflow wheel for my Python version from https://github.com/fo40225/tensorflow-windows-wheel using pip
However, when trying to import tensorflow, I get the following error:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow\__init__.py", line 98, in <module>
from tensorflow_core import *
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\__init__.py", line 45, in <module>
from . _api.v2 import compat
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\_api\v2\compat\__init__.py", line 23, in <module>
from . import v1
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_core\_api\v2\compat\v1\__init__.py", line 664, in <module>
from tensorflow_estimator.python.estimator.api._v1 import estimator
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\__init__.py", line 10, in <module>
from tensorflow_estimator._api.v1 import estimator
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\_api\v1\estimator\__init__.py", line 10, in <module>
from tensorflow_estimator._api.v1.estimator import experimental
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\_api\v1\estimator\experimental\__init__.py", line 10, in <module>
from tensorflow_estimator.python.estimator.canned.dnn import dnn_logit_fn_builder
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\canned\dnn.py", line 33, in <module>
from tensorflow_estimator.python.estimator import estimator
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\estimator.py", line 53, in <module>
from tensorflow_estimator.python.estimator import util as estimator_util
File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\tensorflow_estimator\python\estimator\util.py", line 75, in <module>
class _DatasetInitializerHook(tf.compat.v1.train.SessionRunHook):
AttributeError: module 'tensorflow' has no attribute 'compat'
I have searched, but not found anybody who encountered the same issue using tensorflow wheel (just trying to install tensorflow via "pip install tensorflow" isn't going to work for me, as I will run into the ImportError: DLL load failed-error).
It seems tensorflow is not installed in your system in the proper way which causes this error.
You need to install tensorflow using any of the below command:
pip install tensorflow
pip install tensorflow-gpu # to install tensorflow for gpu support
pip install tensorflow==2.4.0 # to install a specific tensorflow version
For Anaconda environment in jupyter notebook, use below code:
conda install tensorflow
conda install tensorflow-gpu # to install tensorflow for gpu support
Please follow this link which has step wise instructions to install tensorflow in your system.
When running my python code, i get the following error,
File "run_webcam.py", line 8, in <module>
from tf_pose.estimator import TfPoseEstimator
File __init__.py", line 5, in <module>
from tf_pose.runner import infer, Estimator, get_estimator
File runner.py", line 8, in <module>
from tf_pose import eval
File eval.py", line 14, in <module>
from tf_pose.networks import model_wh, get_graph_path
File networks.py", line 6, in <module>
from tf_pose.network_mobilenet import MobilenetNetwork
File network_mobilenet.py", line 5, in <module>
from tf_pose import network_base
File network_base.py", line 8, in <module>
import tensorflow.contrib.slim as slim
ModuleNotFoundError: No module named 'tensorflow.contrib'
I think that i need to import the dependencies differently as i am using tensorflow 2.0.0 and python 3.7.3.
how do i for example change the slim import to work correctly?
Run following commands to solve this issue:
conda create -n tfpose python=3.6 pip
conda activate tfpose
pip uninstall tensorflow
pip install tensorflow
conda install tensorflow
Now execute your python code.