45

I am following this video. At 22:22, an error was found in this code:

!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

Xiddoc
  • 3,369
  • 3
  • 11
  • 37
Bachir bessei
  • 451
  • 1
  • 3
  • 3

16 Answers16

86

Follow these steps:

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

pip install --upgrade protobuf

  1. Copy builder.py from .../Lib/site-packages/google/protobuf/internal to another folder on your computer (let's say 'Documents')
  2. Install a protobuf version that is compatible with your project (for me 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
Eric Pedley
  • 103
  • 9
user19266443
  • 869
  • 2
  • 2
  • 3
    wow smart solution! – big boy Jul 11 '22 at 15:04
  • 5
    You just saved my monitor and keyboard. I am going through this tutorial (https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html#tensorflow-object-detection-api-installation). Finally your solution works! – suchoss Jul 21 '22 at 13:15
  • 9
    protobuf is the worsest software in this world, eveyone rely on it, every version it breaks it's own API. – Nicholas Jela Jul 22 '22 at 07:29
  • 3
    what a great solution... – Ninad Kulkarni Aug 26 '22 at 09:12
  • I had protobuf 3.17.3 in my project and it gave another error after this one, but changing it to 3.19.4 same as you worked well – OanaM Oct 27 '22 at 10:26
  • This solution is amazing. Helped a lot. – Joshua Oct 31 '22 at 00:58
  • 2
    protobuff ws itself a hack to provide hack across communication and this is next hack, damn – TheExorcist Jan 11 '23 at 10:53
  • @NicholasJela just like billion chimps and gimps rely on air to breath, proto is the same for infra, give it some time and you will get the essence. – TheExorcist Jan 11 '23 at 10:54
  • this solution doesn't work for me....anyway, software engineer can not count tricks for solid software, what i can say is protobuf sucks an package management, hope there will be replacement – shan Jun 07 '23 at 05:57
33

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.

marcoc88
  • 681
  • 6
  • 6
17

For some reason, the builder.py file is missing from the protobuf package created from TensorFlow. A workaround is to simply copy the latest copy of builder.py from the protobuf repository into your local drive

wget https://raw.githubusercontent.com/protocolbuffers/protobuf/main/python/google/protobuf/internal/builder.py -O [PROTOBUF_PATH]/lib/python3.8/site-packages/google/protobuf/internal/builder.py
mrtpk
  • 1,398
  • 4
  • 18
  • 38
Victor Medina
  • 171
  • 1
  • 5
7

Solution with two steps

  1. pip uninstall protobuf

  2. pip install protobuf==4.21

Vy Do
  • 46,709
  • 59
  • 215
  • 313
Shams
  • 71
  • 1
  • 2
4

just like Victor answered. When working with tensforflow for somereason when installing protobuf builder.py is missing in the related package.

https://raw.githubusercontent.com/protocolbuffers/protobuf/main/python/google/protobuf/internal/builder.py

copied and pasted this code and made it into a .py then took that file and pasted it in {your env location}/site-packages/google/protobuf/internal/builder.py

and everything has worked.

Khalid A
  • 51
  • 2
  • Added `RUN curl https://raw.githubusercontent.com/protocolbuffers/protobuf/main/python/google/protobuf/internal/builder.py > /usr/local/lib/python3.10/site-packages/google/protobuf/internal/builder.py` to my dockerfile – Daniel Dec 22 '22 at 15:02
2

Facing the same issue while following the Object Detection TensorFlow tutorial on the "Test Installation" tab.

As mentioned above, dunno why protobuff 3.19.6 does not brings builder.py, so you just have to upgrade to to a higher version (lets say 4.21.1) copy builder.py and then downgrading to a tensorFlow compatible protobuff (in my case 3.19.6) and paste it. In my case I was using conda so, builder.py file location was:

C:\Users\ {userX}\miniconda3\envs\ {envname}\Lib\site-packages\google\protobuf\internal

After that, the test run smoothly =).

Best!

2

Downgrading protobuf to 3.20 has solved this problem in my case:

sudo pip3 uninstall protobuf
pip3 install protobuf==3.20
Bilal
  • 3,191
  • 4
  • 21
  • 49
1

what worked for me:

  1. pip uninstall protobuf
  2. conda install protobuf

(im using miniconda3)

user599116
  • 11
  • 1
  • 2
    It could be helpful if posted the version you installed. – AlexK Oct 03 '22 at 19:54
  • 1
    This worked for me. While using pip it was downloading version 4.20 and when i did conda install it installed version 3.20.1 – Hozeis Dec 01 '22 at 13:11
0

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).

muxamilian
  • 342
  • 2
  • 11
0

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"

lxyscls
  • 297
  • 1
  • 3
  • 17
0

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

0

I use Anaconda Prompt and Environment on Windows 11.

I solved the problem by making the same version of the two Protobuf installs one is the Anaconda Protobuf install, and the other one is that I installed from https://github.com/protocolbuffers/protobuf/releases

In order to make the same version, I reinstall Protobuf releases that are compatible with the Anaconda Protoc installation.

You can see what is your Protobuf with pip list

Ahmet Ekiz
  • 31
  • 4
0

I was able to resolve this issue by upgrading the pip version.

pip install --upgrade pip

If you are stuck on an old version of Python then you can create a virtual environment with a more recent version and run pip through Python, i.e.

python -m pip install --upgrade protobuf

or

python -m pip uninstall protobuf
python -m pip install protobuf
Community
  • 1
  • 1
grofte
  • 1,839
  • 1
  • 16
  • 15
0

Try this :

pip uninstall protobuf

pip install protobuf==3.20.3

Worked for me

0

You probably may have installed multiple on your machine especially if you had tried with both conda and pip. You'll be fine with Conda if you uninstall pip. Vis-a-vis.

Jibrin
  • 1
  • 1
-1

yeah, i use python 3.8. i saw https://grpc.io/docs/languages/python/quickstart/ and run the example code but it was failed.

there are many causes, i know. well, no silver bullet! :}

i solved it with upgrade protobuf