【问题标题】:How to solve "AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key"?如何解决“AttributeError:模块'google.protobuf.descriptor'没有属性'_internal_create_key”?
【发布时间】:2020-09-07 08:45:11
【问题描述】:

我在 jupyter notebook 中执行from object_detection.utils import label_map_util 时遇到了它。其实就是tensorflow对象检测教程笔记本(自带tensorflow对象检测api) 完整的错误日志:

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'

【问题讨论】:

    标签: python tensorflow protocol-buffers object-detection-api proto


    【解决方案1】:

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

    在我升级 pip 版本之后

    pip install --upgrade protobuf
    

    问题解决了。

    【讨论】:

    • 如果是python3,使用pip3 install --upgrade protobuf
    【解决方案2】:

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

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

    【讨论】:

    • 就我而言,我只需要将 pip 和 protobuf(在虚拟环境中)从 3.11.3 升级到 3.13.0
    • 对我来说只需将 protobuf 从 3.11.2 更新到 3.13.0 就足够了。
    • 它也适用于我。虽然我升级了protobuf,但还是不行。然后我运行以下评论 pip uninstall protobuf python3-protobuf 并且它起作用了。谢谢!
    【解决方案3】:

    在尝试了许多不同的解决方案(我在 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 中突出显示的那样

    【讨论】:

      【解决方案4】:

      对于其他研究这个最近更新的谷歌库正在使用 proto plus,一个 python proto 消息的包装器。使用这个辅助函数对我有用(信用: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

      【讨论】:

        【解决方案5】:

        对我来说,问题是我有 libprotobuf 3.11protobuf 3.15 升级 libprotobuf 为我修复了它。

        【讨论】:

        • pypi上没有叫libprotobuf的包?
        • 我认为它在 anaconda 上,我没有明确安装它我认为它已作为其他依赖项安装。
        • 这对我有用。具体来说,通过conda install -c conda-forge libprotobuf=3.15。我必须指定版本并升级它。
        【解决方案6】:

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

        pip uninstall pyhton3-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 版本)

        【讨论】:

          【解决方案7】:

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

          conda install -c conda-forge streamlit
          

          使用 3.11.4 的 protobuflibprotobuf 版本

          我无法更新protobuflibprotobuf

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

          conda install -c conda-forge streamlit
          

          我现在得到了 3.14.0 的 protobuflibprotobuf 版本,它可以工作了。

          【讨论】:

            【解决方案8】:

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

            Linux 操作系统

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

            Mac 操作系统

            import my_module_pb2
            

            【讨论】:

              【解决方案9】:

              我的问题是protoc没有安装。

              解决问题的步骤:

              1. 检查pip3 show protobuf,您将看到protobuf 的版本。
                • 等式Version: 3.17.3
              2. 转到protobuf release page 并安装相同版本的protoc
                • 等式
                  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. 如果不起作用,请重新启动并重试。

              【讨论】:

                猜你喜欢
                • 2021-06-10
                • 2021-10-15
                • 2021-12-27
                • 2022-06-21
                • 2022-07-18
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多