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

Viewed 13072

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

6 Answers

Follow these steps:

  1. Install the latest protobuf (in my case is 4.21.1)

pip install protobuf

  1. Copy builder.py from ...\Lib\site-packages\google\protobuf\internal to your computer (let's say 'Documents')
  2. Install protobuf that compatible to your project, for me is 3.19.4

pip install protobuf==3.19.4

  1. Copy builder.py from (let's say 'Documents') to Lib\site-packages\google\protobuf\internal
  2. run your code

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.

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"

Copy file builder.py from where your installing python or if your using ananconda may the builder.py in C:\Users\bachir\anaconda3\Lib\site-packages\google\protobuf\internal, copy it then paste to C:\Users\bachir\PycharmProjects\pythonProject\venv\lib\site-packages\google\protobuf\internal

Solution with two step:

  1. pip uninstall protobuf

  2. pip install protobuf==4.21

Related