ImportError: cannot import name ‘builder’ from ‘google.protobuf.internal’

We Are Going To Discuss About ImportError: cannot import name ‘builder’ from ‘google.protobuf.internal’. So lets Start this Python Article.

ImportError: cannot import name ‘builder’ from ‘google.protobuf.internal’

  1. How to solve ImportError: cannot import name 'builder' from 'google.protobuf.internal'

    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.

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

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

Original Author marcoc88 Of This Content

Solution 2

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

Original Author user19266443 Of This Content

Solution 3

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

Original Author muxamilian Of This Content

Solution 4

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”

Original Author lxyscls Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment