How to solve "AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key"?

How to solve "AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key"?

我在jupyter notebook中执行from object_detection.utils import label_map_util时遇到了。其实就是tensorflow object detection tutorial notebook(自带tensorflow object detectionapi) 完整的错误日志:

AttributeError                            Traceback (most recent call last)
<ipython-input-7-7035655b948a> in <module>
      1 from object_detection.utils import ops as utils_ops
----> 2 from object_detection.utils import label_map_util
      3 from object_detection.utils import visualization_utils as vis_util

~\AppData\Roaming\Python\Python37\site-packages\object_detection\utils\label_map_util.py in <module>
     25 import tensorflow as tf
     26 from google.protobuf import text_format
---> 27 from object_detection.protos import string_int_label_map_pb2
     28 
     29 

~\AppData\Roaming\Python\Python37\site-packages\object_detection\protos\string_int_label_map_pb2.py in <module>
     19   syntax='proto2',
     20   serialized_options=None,
---> 21   create_key=_descriptor._internal_create_key,
     22   serialized_pb=b'\n2object_detection/protos/string_int_label_map.proto\x12\x17object_detection.protos\"\xc0\x01\n\x15StringIntLabelMapItem\x12\x0c\n\x04name\x18\x01 \x01(\t\x12\n\n\x02id\x18\x02 \x01(\x05\x12\x14\n\x0c\x64isplay_name\x18\x03 \x01(\t\x12M\n\tkeypoints\x18\x04 \x03(\x0b\x32:.object_detection.protos.StringIntLabelMapItem.KeypointMap\x1a(\n\x0bKeypointMap\x12\n\n\x02id\x18\x01 \x01(\x05\x12\r\n\x05label\x18\x02 \x01(\t\"Q\n\x11StringIntLabelMap\x12<\n\x04item\x18\x01 \x03(\x0b\x32..object_detection.protos.StringIntLabelMapItem'
     23 )

AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'

我通过pip show protobufprotoc --version得到的protoc版本不一样。 pip 中的版本有点过时了。

我用

升级pip版本后
pip install --upgrade protobuf

问题已解决。

这三个命令帮我解决了这个问题:

pip uninstall protobuf python3-protobuf
pip install --upgrade pip
pip install --upgrade protobuf

在尝试了许多不同的解决方案后(我正在研究 Mac),对我有用的解决方案是使用以下方法重新安装 protobuf:

PROTOC_ZIP=protoc-3.7.1-osx-x86_64.zip
curl -OL https://github.com/protocolbuffers/protobuf/releases/download/v3.7.1/$PROTOC_ZIP
sudo unzip -o $PROTOC_ZIP -d /usr/local bin/protoc
sudo unzip -o $PROTOC_ZIP -d /usr/local 'include/*'
rm -f $PROTOC_ZIP

this article

中突出显示

对于最近研究这个更新的 google 库的其他人来说,他们正在使用 proto plus,它是 python proto 消息的包装器。使用这个辅助函数对我有用(cred:tobked)

import json

import proto

def proto_message_to_dict(message: proto.Message) -> dict:
    """Helper method to parse protobuf message to dictionary."""
    return json.loads(message.__class__.to_json(message))

https://github.com/googleapis/python-memcache/issues/19

我的问题是 libprotobuf3.11protobuf3.15 升级 libprotobuf 为我修复了它。

这可能是由于您的 IDE(在我的例子中 PyCharm)和站点包中的 protobuf 和 python3-protobuf 版本不正确。在我的案例中解决了以下错误:

pip uninstall python3-protobuf
pip uninstall protobuf

然后终于做

pip install protobuf

此外,如果您有 requirements.txt 或 setup.py 从 pip freeze 检查 protobuf 版本并在您的 requirements.txt 文件中使用相同的版本

pip freeze

(检查 protobuf 版本,并在 requirements.txt 中使用相同的版本)

protobuf==3.15.6(在我的例子中,pip freeze 给了我这个版本的 protobuf)

我在 Anaconda 中也遇到了这个问题。 我安装它:

conda install -c conda-forge streamlit

版本 protobuflibprotobuf 为 3.11.4

我无法更新 protobuflibprotobuf

我建议先安装“conda install protobuf”然后

conda install -c conda-forge streamlit

我现在得到了 3.14.0 的 protobuflibprotobuf 版本并且可以正常工作。

如果您使用 protoc 在与导入它的位置不同的 OS 中生成特定于语言的绑定文件,也会发生这种情况。例如,

Linux OS

protoc -I=./ --python_out=./ my_module.proto 

Mac OS

import my_module_pb2

我的问题是protoc没有安装

解决问题的步骤:

  1. 检查 pip3 show protobuf,您将看到 protobuf 的版本。
    • e.q。 Version: 3.17.3
  2. 转到 protobuf release page 并安装相同版本的 protoc
    • e.q。
      1. wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip
      2. unzip protoc-3.17.3-linux-x86_64.zip
      3. vim ~/.bashprofile 并粘贴 PATH=$PATH:/home/YOUR_HOST_NAME/bin
      4. source ~/.bashprofileecho $PATH 检查 /home/YOUR_HOST_NAME/bin 是否存在。
  3. 检查 pip3 show protobuf 等于 protoc --version
  4. 如果不行,重启后重试。