AttributeError: module 'tensorflow' has no attribute 'compat' - python

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.

Related

TensorFlow ModuleNotFoundError: No module named 'official.nlp.bert'

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.

ModuleNotFoundError: No module named 'keras.api' error

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.

Jupiter notebook run error: AttributeError: module 'importlib_metadata' has no attribute 'version'

Originally, Jupyter notebook was running well without any problems.
But after installing tensorflow and keras today, it doesn't work.
Error message:
Traceback (most recent call last):
File "C:\Anaconda3\Scripts\jupyter-notebook-script.py", line 6, in <module>
from notebook.notebookapp import main
File "C:\Anaconda3\lib\site-packages\notebook\notebookapp.py", line 85, in <module>
from .services.contents.manager import ContentsManager
File "C:\Anaconda3\lib\site-packages\notebook\services\contents\manager.py", line 17, in <module>
from nbformat import sign, validate as validate_nb, ValidationError
File "C:\Anaconda3\lib\site-packages\nbformat\__init__.py", line 32, in <module>
from .validator import validate, ValidationError
File "C:\Anaconda3\lib\site-packages\nbformat\validator.py", line 12, in <module>
from .json_compat import get_current_validator, ValidationError
File "C:\Anaconda3\lib\site-packages\nbformat\json_compat.py", line 10, in <module>
import jsonschema
File "C:\Anaconda3\lib\site-packages\jsonschema\__init__.py", line 34, in <module>
__version__ = metadata.version("jsonschema")
AttributeError: module 'importlib_metadata' has no attribute 'version'
There is a problem with your importlib_metadata package, you can try uninstall it first and then forcing out a new installation with the following commands:
pip uninstall importlib_metadata
pip install importlib_metadata --force-reinstall
It worked for me.
This is a problem with the Anaconda version upgrade.
You can solve this with the following command that worked for me.
conda install -c conda-forge importlib_metadata
For me, the same error happened after VSCode proposed me to install a tensorboard plugin and I pressed yes. (MacOS 11.4, Conda 4.10.3)
It appears that it introduced some kind of version mismatch for multiple packages in the environment.
The following fixed the problem:
conda update --all

Import librosa gives "no module named numba.decorators", how to solve?

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.

Using tenorflow and python-not finding modules

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.

Categories