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’
- 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 abuilder
module, which it cannot find if you haven't updated to the latest version of theprotobuf
package. This is not explained in the release notes, but I verified myself that it works if you upgrade theprotobuf
package. - 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 abuilder
module, which it cannot find if you haven't updated to the latest version of theprotobuf
package. This is not explained in the release notes, but I verified myself that it works if you upgrade theprotobuf
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:
- Install the latest protobuf (in my case is 4.21.1)
pip install protobuf
- Copy
builder.py
from...\Lib\site-packages\google\protobuf\internal
to your computer (let’s say ‘Documents’) - Install protobuf that compatible to your project, for me is 3.19.4
pip install protobuf==3.19.4
- Copy builder.py from (let’s say ‘Documents’) to
Lib\site-packages\google\protobuf\internal
- 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.