'I'm facing an error while importing tensorflow lately

I'm trying to import the below packages , it was working previously , today all of sudden I'm unable to install these packages

import tensorflow
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from sklearn.model_selection import train_test_split
from keras.layers.pooling import AveragePooling2D
from keras.layers.core import Dropout
from keras.layers.core import Flatten
from keras.layers.core import Dense
from sklearn.preprocessing import LabelBinarizer
from keras.layers import Input
from tensorflow.keras.models import Model
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import load_model
from sklearn.metrics import classification_report
from keras.applications import VGG19
from imutils import paths
from collections import deque
import matplotlib.pyplot as plt
import numpy as np
import cv2
import os
import pickle

But facing the below error

--------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-8-c04f6b1075c2> in <module>
----> 1 import tensorflow
      2 from tensorflow.keras.preprocessing.image import ImageDataGenerator
      3 from sklearn.model_selection import train_test_split
      4 from keras.layers.pooling import AveragePooling2D
      5 from keras.layers.core import Dropout

~\anaconda3\lib\site-packages\tensorflow\__init__.py in <module>
     39 import sys as _sys
     40 
---> 41 from tensorflow.python.tools import module_util as _module_util
     42 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     43 

~\anaconda3\lib\site-packages\tensorflow\python\__init__.py in <module>
     44 
     45 # Bring in subpackages.
---> 46 from tensorflow.python import data
     47 from tensorflow.python import distribute
     48 from tensorflow.python import keras

~\anaconda3\lib\site-packages\tensorflow\python\data\__init__.py in <module>
     23 
     24 # pylint: disable=unused-import
---> 25 from tensorflow.python.data import experimental
     26 from tensorflow.python.data.ops.dataset_ops import AUTOTUNE
     27 from tensorflow.python.data.ops.dataset_ops import Dataset

~\anaconda3\lib\site-packages\tensorflow\python\data\experimental\__init__.py in <module>
     97 
     98 # pylint: disable=unused-import
---> 99 from tensorflow.python.data.experimental import service
    100 from tensorflow.python.data.experimental.ops.batching import dense_to_ragged_batch
    101 from tensorflow.python.data.experimental.ops.batching import dense_to_sparse_batch

~\anaconda3\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py in <module>
    138 from __future__ import print_function
    139 
--> 140 from tensorflow.python.data.experimental.ops.data_service_ops import distribute
    141 from tensorflow.python.data.experimental.ops.data_service_ops import from_dataset_id
    142 from tensorflow.python.data.experimental.ops.data_service_ops import register_dataset

~\anaconda3\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py in <module>
     23 
     24 from tensorflow.python import tf2
---> 25 from tensorflow.python.data.experimental.ops import compression_ops
     26 from tensorflow.python.data.experimental.ops.distribute_options import AutoShardPolicy
     27 from tensorflow.python.data.experimental.ops.distribute_options import ExternalStatePolicy

~\anaconda3\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py in <module>
     18 from __future__ import print_function
     19 
---> 20 from tensorflow.python.data.util import structure
     21 from tensorflow.python.ops import gen_experimental_dataset_ops as ged_ops
     22 

~\anaconda3\lib\site-packages\tensorflow\python\data\util\structure.py in <module>
     24 import wrapt
     25 
---> 26 from tensorflow.python.data.util import nest
     27 from tensorflow.python.framework import composite_tensor
     28 from tensorflow.python.framework import ops

~\anaconda3\lib\site-packages\tensorflow\python\data\util\nest.py in <module>
     38 import six as _six
     39 
---> 40 from tensorflow.python.framework import sparse_tensor as _sparse_tensor
     41 from tensorflow.python.util import _pywrap_utils
     42 from tensorflow.python.util import nest

~\anaconda3\lib\site-packages\tensorflow\python\framework\sparse_tensor.py in <module>
     26 from tensorflow.python import tf2
     27 from tensorflow.python.framework import composite_tensor
---> 28 from tensorflow.python.framework import constant_op
     29 from tensorflow.python.framework import dtypes
     30 from tensorflow.python.framework import ops

~\anaconda3\lib\site-packages\tensorflow\python\framework\constant_op.py in <module>
     27 from tensorflow.core.framework import types_pb2
     28 from tensorflow.python.eager import context
---> 29 from tensorflow.python.eager import execute
     30 from tensorflow.python.framework import dtypes
     31 from tensorflow.python.framework import op_callbacks

~\anaconda3\lib\site-packages\tensorflow\python\eager\execute.py in <module>
     26 from tensorflow.python.eager import core
     27 from tensorflow.python.framework import dtypes
---> 28 from tensorflow.python.framework import ops
     29 from tensorflow.python.framework import tensor_shape
     30 from tensorflow.python.util import compat

~\anaconda3\lib\site-packages\tensorflow\python\framework\ops.py in <module>
     52 from tensorflow.python.framework import c_api_util
     53 from tensorflow.python.framework import composite_tensor
---> 54 from tensorflow.python.framework import cpp_shape_inference_pb2
     55 from tensorflow.python.framework import device as pydev
     56 from tensorflow.python.framework import dtypes

~\anaconda3\lib\site-packages\tensorflow\python\framework\cpp_shape_inference_pb2.py in <module>
     18 
     19 
---> 20 DESCRIPTOR = _descriptor.FileDescriptor(
     21   name='tensorflow/python/framework/cpp_shape_inference.proto',
     22   package='tensorflow',

~\anaconda3\lib\site-packages\google\protobuf\descriptor.py in __new__(cls, name, package, options, serialized_options, serialized_pb, dependencies, public_dependencies, syntax, pool, create_key)
    981           raise RuntimeError('Please link in cpp generated lib for %s' % (name))
    982       elif serialized_pb:
--> 983         return _message.default_pool.AddSerializedFile(serialized_pb)
    984       else:
    985         return super(FileDescriptor, cls).__new__(cls)

TypeError: Couldn't build proto file into descriptor pool!
Invalid proto descriptor for file "tensorflow/python/framework/cpp_shape_inference.proto":
  tensorflow.CppShapeInferenceResult.HandleShapeAndType.specialized_type: ".tensorflow.SpecializedType" is not defined.

Can someone help me resolve this error ??

I uninstalled tensorflow and reinstalled as well Also i uninstalled protobuf reinstalled protobuf as below pip install --no-binary=protobuf protobuf

but still didn't work



Solution 1:[1]

Create a new environment for tensorflow and reinstall tensorflow in that environment using below code:

conda create -n tf tensorflow python=3.5
conda activate tf

conda install pip
pip install tensorflow

Then try again executing your codes. Let us know if still issue persists.

Solution 2:[2]

Are you using a virtual environment if so you need to activate it everytime you run you run your project.If not i strongly suggest you create one for each project

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 TFer2
Solution 2 Dennis Kainga