'ImportError: cannot import name 'builder' from 'google.protobuf.internal'

I am following this video text An error was found in this code, at 22:22

.

!python {SCRIPTS_PATH + '/generate_tfrecord.py'} -x {IMAGE_PATH + '/train'} -l {ANNOTATION_PATH + '/label_map.pbtxt'} -o {ANNOTATION_PATH + '/train.record'}
!python {SCRIPTS_PATH + '/generate_tfrecord.py'} -x{IMAGE_PATH + '/test'} -l {ANNOTATION_PATH + '/label_map.pbtxt'} -o {ANNOTATION_PATH + '/test.record'}

. Post exact error message,

Traceback (most recent call last):
  File "D:\info\1 Master\2 semster\RealTimeObjectDetection-main\Tensorflow\scripts\generate_tfrecord.py", line 29, in <module>
    from object_detection.utils import dataset_util, label_map_util
  File "C:\Users\bachir\PycharmProjects\pythonProject\venv\lib\site-packages\object_detection\utils\label_map_util.py", line 29, in <module>
    from object_detection.protos import string_int_label_map_pb2
  File "C:\Users\bachir\PycharmProjects\pythonProject\venv\lib\site-packages\object_detection\protos\string_int_label_map_pb2.py", line 5, in <module>
    from google.protobuf.internal import builder as _builder
ImportError: cannot import name 'builder' from 'google.protobuf.internal' (C:\Users\bachir\PycharmProjects\pythonProject\venv\lib\site-packages\google\protobuf\internal\__init__.py)

.

First I tried to download some missing packages but same problem, I would like to help



Solution 1:[1]

You need to upgrade to the latest version of the protobuf package:

pip install --upgrade protobuf.

The reason is that the Python classes are simplified since Protobuf v3.20.0. Straight from the release notes it says:

Protobuf python generated codes are simplified. Descriptors and message classes' definitions are now dynamic created in internal/builder.py. Insertion Points for messages classes are discarded.

This explains why the generated Python code now refers to a builder module, which it cannot find if you haven't updated to the latest version of the protobuf package. This is not explained in the release notes, but I verified myself that it works if you upgrade the protobuf package.

Solution 2:[2]

I could solve the issue by not compiling my .proto files with the newest version of the protoc compiler but by using the old version v3.19.4 (see https://github.com/protocolbuffers/protobuf/releases).

Solution 3:[3]

Because descriptor_pb2.py is generated from protoc. So you need to keep the compatibility between your buiding system and running system.

In one word, make sure that your protoc's version is less than or equal to protobuf's version.

FYI, you can download the according protoc directly from "https://github.com/protocolbuffers/protobuf/releases"

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 marcoc88
Solution 2 muxamilian
Solution 3 lxyscls