如何解决"AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key"?



我在jupyter笔记本中执行from object_detection.utils import label_map_util时遇到了它。它实际上是张量流对象检测教程笔记本(它附带了张量流对象检测 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
~AppDataRoamingPythonPython37site-packagesobject_detectionutilslabel_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 
~AppDataRoamingPythonPython37site-packagesobject_detectionprotosstring_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.protox12x17object_detection.protos"xc0x01nx15StringIntLabelMapItemx12x0cnx04namex18x01 x01(tx12nnx02idx18x02 x01(x05x12x14nx0cx64isplay_namex18x03 x01(tx12Mntkeypointsx18x04 x03(x0bx32:.object_detection.protos.StringIntLabelMapItem.KeypointMapx1a(nx0bKeypointMapx12nnx02idx18x01 x01(x05x12rnx05labelx18x02 x01(t"Qnx11StringIntLabelMapx12<nx04itemx18x01 x03(x0bx32..object_detection.protos.StringIntLabelMapItem'
23 )
AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key'

我通过pip show protobufprotoc --version获得的原型版本是不同的。pip 中的版本有点过时了。

在我升级了pip版本之后

pip install --upgrade protobuf

问题解决了。

这三个命令为我解决了它:

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

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

pip uninstall python3-protobuf
pip uninstall protobuf

然后最后做

pip install protobuf

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

pip freeze

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

protobuf==3.15.6(在我的情况下,pip freeze给了我这个版本的protobuf(

对于最近研究这个问题的其他人,更新的谷歌库正在使用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

在尝试了许多不同的解决方案(我在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

正如本文所强调的

对我来说的问题是我在3.11libprotobufprotobuf在升级libprotobuf时3.15为我修复了它。

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

conda install -c conda-forge streamlit

使用 3.11.4 的protobuflibprotobuf版本

我既无法更新protobuf也无法更新libprotobuf.

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

conda install -c conda-forge streamlit

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

我的条件问题是protoc不安装。

解决问题的步骤:

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

使用 pip 降级 protobuf

有时升级代码pip install --upgrade protobuf抛出另一个错误。

  • 版本 3.20 似乎工作正常
pip install protobuf==3.20.*

类似于类型错误:不能直接创建描述符

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

Linux操作系统

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

苹果操作系统

import my_module_pb2

就我而言,我正在将字典传递给Google云API期望字符串的位置。呜呜。

相关内容

最新更新