在用python引用protobuf编译好的.py文件时遇到报错:
File "/home/yany/.local/lib/python2.7/site-packages/google/protobuf/message.py", line 199, in ParseFromString
return self.MergeFromString(serialized)
File "/home/yany/.local/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 1134, in MergeFromString
raise message_mod.DecodeError('Truncated message.')
google.protobuf.message.DecodeError: Truncated message.
网上没查到很合适的解决方案,debug过程中发现把.proto文件中的空行去掉会有一定改善,但不治本;
最后发现是proto版本问题,
proto的python版本和C版本要匹配,我这边的版本信息:
proto的python版本:3.17.3
安装指令:
默认:pip install protobuf
指定版本:pip install protobuf==3.17.3
版本查询方法:
python
>>>import google.protobuf
>>>google.protobuf.__version__
安装c++版本protobuf, c++版本应该是protoc 3.0.0,
版本查询命令 protoc --version
博客讲述了在使用Python引用protobuf编译后的.py文件时遇到的'google.protobuf.message.DecodeError: Truncated message.'错误。经过排查,问题根源在于protobuf的Python版本(3.17.3)与C++版本(protoc 3.0.0)不匹配。解决方法是确保两者版本一致。检查和安装protobuf版本的命令分别为`python -c 'import google.protobuf; print(google.protobuf.__version__)`和`protoc --version`。

311

被折叠的 条评论
为什么被折叠?



