Import tensorflow as tf won't finish running

I try to run this code right here (note I am on python 3.9.12):

import tensorflow as tf
from object_detection.utils import config_util
from object_detection.protos import pipeline_pb2
from google.protobuf import text_format

it won’t stop running and when I interrupt it it gives me this output:

---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
Cell In[13], line 5
      1 #!pwd
      2 #print (PYTHONPATH)
      3 #print(os.path)
----> 5 import tensorflow as tf
      6 from object_detection.utils import config_util
      7 from object_detection.protos import pipeline_pb2

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/tensorflow/__init__.py:54
     52 from tensorflow._api.v2 import autograph
     53 from tensorflow._api.v2 import bitwise
---> 54 from tensorflow._api.v2 import compat
     55 from tensorflow._api.v2 import config
     56 from tensorflow._api.v2 import data

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/tensorflow/_api/v2/compat/__init__.py:8
      3 """Public API for tf._api.v2.compat namespace
      4 """
      6 import sys as _sys
----> 8 from tensorflow._api.v2.compat import v1
      9 from tensorflow._api.v2.compat import v2
     10 from tensorflow.python.compat.compat import forward_compatibility_horizon # line: 125

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/tensorflow/_api/v2/compat/v1/__init__.py:32
     30 from tensorflow._api.v2.compat.v1 import autograph
     31 from tensorflow._api.v2.compat.v1 import bitwise
---> 32 from tensorflow._api.v2.compat.v1 import compat
     33 from tensorflow._api.v2.compat.v1 import config
     34 from tensorflow._api.v2.compat.v1 import data

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/tensorflow/_api/v2/compat/v1/compat/__init__.py:9
      6 import sys as _sys
      8 from tensorflow._api.v2.compat.v1.compat import v1
----> 9 from tensorflow._api.v2.compat.v1.compat import v2
     10 from tensorflow.python.compat.compat import forward_compatibility_horizon # line: 125
     11 from tensorflow.python.compat.compat import forward_compatible # line: 65

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/tensorflow/_api/v2/compat/v1/compat/v2/__init__.py:28
     25 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     26 from tensorflow.python.util.lazy_loader import KerasLazyLoader as _KerasLazyLoader
---> 28 from tensorflow._api.v2.compat.v2 import __internal__
     29 from tensorflow._api.v2.compat.v2 import __operators__
     30 from tensorflow._api.v2.compat.v2 import audio

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/tensorflow/_api/v2/compat/v2/__init__.py:28
     25 from tensorflow.python.util.lazy_loader import LazyLoader as _LazyLoader
     26 from tensorflow.python.util.lazy_loader import KerasLazyLoader as _KerasLazyLoader
---> 28 from tensorflow._api.v2.compat.v2 import __internal__
     29 from tensorflow._api.v2.compat.v2 import __operators__
     30 from tensorflow._api.v2.compat.v2 import audio

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/tensorflow/_api/v2/compat/v2/__internal__/__init__.py:10
      8 from tensorflow._api.v2.compat.v2.__internal__ import autograph
      9 from tensorflow._api.v2.compat.v2.__internal__ import decorator
---> 10 from tensorflow._api.v2.compat.v2.__internal__ import dispatch
     11 from tensorflow._api.v2.compat.v2.__internal__ import distribute
     12 from tensorflow._api.v2.compat.v2.__internal__ import eager_context

File <frozen importlib._bootstrap>:1176, in _find_and_load(name, import_)

File <frozen importlib._bootstrap>:1147, in _find_and_load_unlocked(name, import_)

File <frozen importlib._bootstrap>:690, in _load_unlocked(spec)

File <frozen importlib._bootstrap_external>:936, in exec_module(self, module)

File <frozen importlib._bootstrap_external>:1032, in get_code(self, fullname)

File <frozen importlib._bootstrap_external>:1131, in get_data(self, path)

KeyboardInterrupt: 

It should just move on without any output but instead it won’t stop running. I have used the exact python version before. In fact most of the depends should be the same however I am still unable to run this block of code. Any help is appreciated!

Hi @20Spaces, The error was generated due to keyboard interruption. Could you please wait for some time until the execution was completed. I have tried to import above libraries in colab and did not take much time to execute
image

If the execution takes more time, could you please let us know the tensorflow, python versions you are using. Also please provide the details like OS, environment in which you are trying to execute the code. Thank You.

I purposely stoped the program after waiting a few minutes.
My python version: 3.9.12
OS: macOS newest version
Exciting code in Jupyter notebook within a virtual environment
TF verson: 2.13.0rc0

Update: I have created a new virtual environment and the issue is resolved but a new issue started appearing:

`---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[29], line 2
1 import tensorflow as tf
----> 2 from object_detection.utils import config_util
3 from object_detection.protos import pipeline_pb2
4 from google.protobuf import text_format

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/object_detection/utils/config_util.py:24
20 from google.protobuf import text_format
22 from “modified by moderator” import file_io
—> 24 from object_detection.protos import eval_pb2
25 from object_detection.protos import graph_rewriter_pb2
26 from object_detection.protos import input_reader_pb2

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/object_detection/protos/eval_pb2.py:36
13 _sym_db = _symbol_database.Default()
18 DESCRIPTOR = _descriptor.FileDescriptor(
19 name=‘object_detection/protos/eval.proto’,
20 package=‘object_detection.protos’,
(…)
23 serialized_pb=_b(‘\n"object_detection/protos/eval.proto\x12\x17object_detection.protos"\xf7\x05\n\nEvalConfig\x12\x15\n\nbatch_size\x18\x19 \x01(\r:\x01\x31\x12\x1e\n\x12num_visualizations\x18\x01 \x01(\r:\x02\x31\x30\x12\x1e\n\x0cnum_examples\x18\x02 \x01(\r:\x04\x35\x30\x30\x30\x42\x02\x18\x01\x12\x1f\n\x12\x65val_interval_secs\x18\x03 \x01(\r:\x03\x33\x30\x30\x12\x18\n\tmax_evals\x18\x04 \x01(\r:\x01\x30\x42\x02\x18\x01\x12\x19\n\nsave_graph\x18\x05 \x01(\x08:\x05\x66\x61lse\x12"\n\x18visualization_export_dir\x18\x06 \x01(\t:\x00\x12\x15\n\x0b\x65val_master\x18\x07 \x01(\t:\x00\x12\x13\n\x0bmetrics_set\x18\x08 \x03(\t\x12\x15\n\x0b\x65xport_path\x18\t \x01(\t:\x00\x12!\n\x12ignore_groundtruth\x18\n \x01(\x08:\x05\x66\x61lse\x12"\n\x13use_moving_averages\x18\x0b \x01(\x08:\x05\x66\x61lse\x12"\n\x13\x65val_instance_masks\x18\x0c \x01(\x08:\x05\x66\x61lse\x12 \n\x13min_score_threshold\x18\r \x01(\x02:\x03\x30.5\x12&\n\x1amax_num_boxes_to_visualize\x18\x0e \x01(\x05:\x02\x32\x30\x12\x1a\n\x0bskip_scores\x18\x0f \x01(\x08:\x05\x66\x61lse\x12\x1a\n\x0bskip_labels\x18\x10 \x01(\x08:\x05\x66\x61lse\x12*\n\x1bvisualize_groundtruth_boxes\x18\x11 \x01(\x08:\x05\x66\x61lse\x12\x32\n#groundtruth_box_visualization_color\x18\x12 \x01(\t:\x05\x62lack\x12\x35\n&keep_image_id_for_visualization_export\x18\x13 \x01(\x08:\x05\x66\x61lse\x12$\n\x16retain_original_images\x18\x17 \x01(\x08:\x04true\x12+\n\x1cinclude_metrics_per_category\x18\x18 \x01(\x08:\x05\x66\x61lse’)
24 )
29 _EVALCONFIG = _descriptor.Descriptor(
30 name=‘EvalConfig’,
31 full_name=‘object_detection.protos.EvalConfig’,
32 filename=None,
33 file=DESCRIPTOR,
34 containing_type=None,
35 fields=[
—> 36 _descriptor.FieldDescriptor(
37 name=‘batch_size’, full_name=‘object_detection.protos.EvalConfig.batch_size’, index=0,
38 number=25, type=13, cpp_type=3, label=1,
39 has_default_value=True, default_value=1,
40 message_type=None, enum_type=None, containing_type=None,
41 is_extension=False, extension_scope=None,
42 serialized_options=None, file=DESCRIPTOR),
43 _descriptor.FieldDescriptor(
44 name=‘num_visualizations’, full_name=‘object_detection.protos.EvalConfig.num_visualizations’, index=1,
45 number=1, type=13, cpp_type=3, label=1,
46 has_default_value=True, default_value=10,
47 message_type=None, enum_type=None, containing_type=None,
48 is_extension=False, extension_scope=None,
49 serialized_options=None, file=DESCRIPTOR),
50 _descriptor.FieldDescriptor(
51 name=‘num_examples’, full_name=‘object_detection.protos.EvalConfig.num_examples’, index=2,
52 number=2, type=13, cpp_type=3, label=1,
53 has_default_value=True, default_value=5000,
54 message_type=None, enum_type=None, containing_type=None,
55 is_extension=False, extension_scope=None,
56 serialized_options=_b(‘\030\001’), file=DESCRIPTOR),
57 _descriptor.FieldDescriptor(
58 name=‘eval_interval_secs’, full_name=‘object_detection.protos.EvalConfig.eval_interval_secs’, index=3,
59 number=3, type=13, cpp_type=3, label=1,
60 has_default_value=True, default_value=300,
61 message_type=None, enum_type=None, containing_type=None,
62 is_extension=False, extension_scope=None,
63 serialized_options=None, file=DESCRIPTOR),
64 _descriptor.FieldDescriptor(
65 name=‘max_evals’, full_name=‘object_detection.protos.EvalConfig.max_evals’, index=4,
66 number=4, type=13, cpp_type=3, label=1,
67 has_default_value=True, default_value=0,
68 message_type=None, enum_type=None, containing_type=None,
69 is_extension=False, extension_scope=None,
70 serialized_options=_b(‘\030\001’), file=DESCRIPTOR),
71 _descriptor.FieldDescriptor(
72 name=‘save_graph’, full_name=‘object_detection.protos.EvalConfig.save_graph’, index=5,
73 number=5, type=8, cpp_type=7, label=1,
74 has_default_value=True, default_value=False,
75 message_type=None, enum_type=None, containing_type=None,
76 is_extension=False, extension_scope=None,
77 serialized_options=None, file=DESCRIPTOR),
78 _descriptor.FieldDescriptor(
79 name=‘visualization_export_dir’, full_name=‘object_detection.protos.EvalConfig.visualization_export_dir’, index=6,
80 number=6, type=9, cpp_type=9, label=1,
81 has_default_value=True, default_value=_b(“”).decode(‘utf-8’),
82 message_type=None, enum_type=None, containing_type=None,
83 is_extension=False, extension_scope=None,
84 serialized_options=None, file=DESCRIPTOR),
85 _descriptor.FieldDescriptor(
86 name=‘eval_master’, full_name=‘object_detection.protos.EvalConfig.eval_master’, index=7,
87 number=7, type=9, cpp_type=9, label=1,
88 has_default_value=True, default_value=_b(“”).decode(‘utf-8’),
89 message_type=None, enum_type=None, containing_type=None,
90 is_extension=False, extension_scope=None,
91 serialized_options=None, file=DESCRIPTOR),
92 _descriptor.FieldDescriptor(
93 name=‘metrics_set’, full_name=‘object_detection.protos.EvalConfig.metrics_set’, index=8,
94 number=8, type=9, cpp_type=9, label=3,
95 has_default_value=False, default_value=,
96 message_type=None, enum_type=None, containing_type=None,
97 is_extension=False, extension_scope=None,
98 serialized_options=None, file=DESCRIPTOR),
99 _descriptor.FieldDescriptor(
100 name=‘export_path’, full_name=‘object_detection.protos.EvalConfig.export_path’, index=9,
101 number=9, type=9, cpp_type=9, label=1,
102 has_default_value=True, default_value=_b(“”).decode(‘utf-8’),
103 message_type=None, enum_type=None, containing_type=None,
104 is_extension=False, extension_scope=None,
105 serialized_options=None, file=DESCRIPTOR),
106 _descriptor.FieldDescriptor(
107 name=‘ignore_groundtruth’, full_name=‘object_detection.protos.EvalConfig.ignore_groundtruth’, index=10,
108 number=10, type=8, cpp_type=7, label=1,
109 has_default_value=True, default_value=False,
110 message_type=None, enum_type=None, containing_type=None,
111 is_extension=False, extension_scope=None,
112 serialized_options=None, file=DESCRIPTOR),
113 _descriptor.FieldDescriptor(
114 name=‘use_moving_averages’, full_name=‘object_detection.protos.EvalConfig.use_moving_averages’, index=11,
115 number=11, type=8, cpp_type=7, label=1,
116 has_default_value=True, default_value=False,
117 message_type=None, enum_type=None, containing_type=None,
118 is_extension=False, extension_scope=None,
119 serialized_options=None, file=DESCRIPTOR),
120 _descriptor.FieldDescriptor(
121 name=‘eval_instance_masks’, full_name=‘object_detection.protos.EvalConfig.eval_instance_masks’, index=12,
122 number=12, type=8, cpp_type=7, label=1,
123 has_default_value=True, default_value=False,
124 message_type=None, enum_type=None, containing_type=None,
125 is_extension=False, extension_scope=None,
126 serialized_options=None, file=DESCRIPTOR),
127 _descriptor.FieldDescriptor(
128 name=‘min_score_threshold’, full_name=‘object_detection.protos.EvalConfig.min_score_threshold’, index=13,
129 number=13, type=2, cpp_type=6, label=1,
130 has_default_value=True, default_value=float(0.5),
131 message_type=None, enum_type=None, containing_type=None,
132 is_extension=False, extension_scope=None,
133 serialized_options=None, file=DESCRIPTOR),
134 _descriptor.FieldDescriptor(
135 name=‘max_num_boxes_to_visualize’, full_name=‘object_detection.protos.EvalConfig.max_num_boxes_to_visualize’, index=14,
136 number=14, type=5, cpp_type=1, label=1,
137 has_default_value=True, default_value=20,
138 message_type=None, enum_type=None, containing_type=None,
139 is_extension=False, extension_scope=None,
140 serialized_options=None, file=DESCRIPTOR),
141 _descriptor.FieldDescriptor(
142 name=‘skip_scores’, full_name=‘object_detection.protos.EvalConfig.skip_scores’, index=15,
143 number=15, type=8, cpp_type=7, label=1,
144 has_default_value=True, default_value=False,
145 message_type=None, enum_type=None, containing_type=None,
146 is_extension=False, extension_scope=None,
147 serialized_options=None, file=DESCRIPTOR),
148 _descriptor.FieldDescriptor(
149 name=‘skip_labels’, full_name=‘object_detection.protos.EvalConfig.skip_labels’, index=16,
150 number=16, type=8, cpp_type=7, label=1,
151 has_default_value=True, default_value=False,
152 message_type=None, enum_type=None, containing_type=None,
153 is_extension=False, extension_scope=None,
154 serialized_options=None, file=DESCRIPTOR),
155 _descriptor.FieldDescriptor(
156 name=‘visualize_groundtruth_boxes’, full_name=‘object_detection.protos.EvalConfig.visualize_groundtruth_boxes’, index=17,
157 number=17, type=8, cpp_type=7, label=1,
158 has_default_value=True, default_value=False,
159 message_type=None, enum_type=None, containing_type=None,
160 is_extension=False, extension_scope=None,
161 serialized_options=None, file=DESCRIPTOR),
162 _descriptor.FieldDescriptor(
163 name=‘groundtruth_box_visualization_color’, full_name=‘object_detection.protos.EvalConfig.groundtruth_box_visualization_color’, index=18,
164 number=18, type=9, cpp_type=9, label=1,
165 has_default_value=True, default_value=_b(“black”).decode(‘utf-8’),
166 message_type=None, enum_type=None, containing_type=None,
167 is_extension=False, extension_scope=None,
168 serialized_options=None, file=DESCRIPTOR),
169 _descriptor.FieldDescriptor(
170 name=‘keep_image_id_for_visualization_export’, full_name=‘object_detection.protos.EvalConfig.keep_image_id_for_visualization_export’, index=19,
171 number=19, type=8, cpp_type=7, label=1,
172 has_default_value=True, default_value=False,
173 message_type=None, enum_type=None, containing_type=None,
174 is_extension=False, extension_scope=None,
175 serialized_options=None, file=DESCRIPTOR),
176 _descriptor.FieldDescriptor(
177 name=‘retain_original_images’, full_name=‘object_detection.protos.EvalConfig.retain_original_images’, index=20,
178 number=23, type=8, cpp_type=7, label=1,
179 has_default_value=True, default_value=True,
180 message_type=None, enum_type=None, containing_type=None,
181 is_extension=False, extension_scope=None,
182 serialized_options=None, file=DESCRIPTOR),
183 _descriptor.FieldDescriptor(
184 name=‘include_metrics_per_category’, full_name=‘object_detection.protos.EvalConfig.include_metrics_per_category’, index=21,
185 number=24, type=8, cpp_type=7, label=1,
186 has_default_value=True, default_value=False,
187 message_type=None, enum_type=None, containing_type=None,
188 is_extension=False, extension_scope=None,
189 serialized_options=None, file=DESCRIPTOR),
190 ],
191 extensions=[
192 ],
193 nested_types=,
194 enum_types=[
195 ],
196 serialized_options=None,
197 is_extendable=False,
198 syntax=‘proto2’,
199 extension_ranges=,
200 oneofs=[
201 ],
202 serialized_start=64,
203 serialized_end=823,
204 )
206 DESCRIPTOR.message_types_by_name[‘EvalConfig’] = _EVALCONFIG
207 _sym_db.RegisterFileDescriptor(DESCRIPTOR)

File ~/Desktop/Jupyter/TFODCourse/tfod/lib/python3.11/site-packages/google/protobuf/descriptor.py:561, in FieldDescriptor.new(cls, name, full_name, index, number, type, cpp_type, label, default_value, message_type, enum_type, containing_type, is_extension, extension_scope, options, serialized_options, has_default_value, containing_oneof, json_name, file, create_key)
555 def new(cls, name, full_name, index, number, type, cpp_type, label,
556 default_value, message_type, enum_type, containing_type,
557 is_extension, extension_scope, options=None,
558 serialized_options=None,
559 has_default_value=True, containing_oneof=None, json_name=None,
560 file=None, create_key=None): # pylint: disable=redefined-builtin
→ 561 _message.Message._CheckCalledFromGeneratedFile()
562 if is_extension:
563 return _message.default_pool.FindExtensionByName(full_name)

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: Changes announced May 6, 2022 | Protocol Buffers Documentation
`

Sorry the error is a bit lengthy. How do I go about fixing this problem?

Issue has been resolved thanks for the help!

For those who also have the same issue (and with the solution not being posted here), what fixed my issue is changing “import tensorflow as tf” to “import tensorflow” and then doing a find and replace from “tf” to “tensorflow”. This then fixed the issue and I was able to use “import tensorflow as tf” again.