0

I'm using a Mac/Linux and I'm trying to get python code generated from a .proto file. When I run the protoc --version command in my terminal it returns libprotoc 22.5.

When I run a terminal command like

protoc --python_out=. ./path/to/my/proto_file.proto.

I get a python file that auto generates and the first line of the file is

from google.protobuf.internal import builder as _builder

When I try to run that line in my current python environment, I get this error.

Traceback (most recent call last):
  File "/Users/me/opt/miniconda3/envs/my_env/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3505, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-3-9454f45a4b99>", line 1, in <module>
    from google.protobuf.internal import builder as _builder
ImportError: cannot import name 'builder' from 'google.protobuf.internal' (/Users/me/opt/miniconda3/envs/my_env/lib/python3.8/site-packages/google/protobuf/internal/__init__.py)

In this SO Question, and several other places, it recommends that you upgrade your version of protobuf to resolve the error. However, due to package dependencies, I can't update my protobuf package to anything over version 3.19.6. I'm assuming that I need to downgrade my protoc version on my mac to match the older python protobuf version so that the auto-generated code will only reference packages/methods/functions that exist in python version 3.19.6. What version should I downgrade protoc to, and how would I do that (I have homebrew installed and can use brew commands)?

Jed
  • 1,823
  • 4
  • 20
  • 52
  • 1
    A new versioning scheme was introduced [Version Support](https://protobuf.dev/support/version-support/). `libprotoc 22.5` corresponds to `4.22.5` (see Pypi `protobuf` [history](https://pypi.org/project/protobuf/#history)). There was also a change made to the Python metaclasses generated by `protoc` but I'm unable to find the documentation of this change. You should be able to revert to `protoc` release [v3.19.6](https://github.com/protocolbuffers/protobuf/releases/tag/v3.19.6) to mirror the Pypi version ([link](https://pypi.org/project/protobuf/3.19.6/)). I don't know how to use `brew`. – DazWilkin Jun 07 '23 at 00:29
  • 1
    It would be prudent to voice your concern to the folks maintaining the dependency/-ies limiting your code to protobuf 3.19.6 as this is almost a year old. You're not only missing out on possible functional improvements and bug fixes but are at increased risk of security vulnerabilities etc. – DazWilkin Jun 07 '23 at 00:34
  • @DazWilkin, Oddly, the package causing the dependency issue is _another_ Google package, Tensorflow. I'm currently using 2.11.1. I'll try bumping up to 2.12.0 to see if that fixes it, but I don't see any mention of protobuf changes in the release notes. – Jed Jun 07 '23 at 15:05
  • 1
    Found it... See the Python synopsis [v3.20.0-rc1](https://github.com/protocolbuffers/protobuf/releases/tag/v3.20.0-rc1) "Protobuf python generated codes are simplified". Releases before this did not use `builder` and the metaclasses in the stubs were different. This appears to be earlier than your issue and so may be unrelated. – DazWilkin Jun 07 '23 at 15:38
  • 1
    After updating _MANY_ things in my python environment, including Tensorflow to 2.12.0, I was able to update python protobuf package to version 4.22.5 and it resolved my error. – Jed Jun 07 '23 at 17:08
  • Sorry I wasn't any help. – DazWilkin Jun 07 '23 at 17:09

0 Answers0