How to solve "AttributeError: module 'google.protobuf.descriptor' has no attribute '_internal_create_key"?
Question:
I encountered it while executing from object_detection.utils import label_map_util
in jupyter notebook. It is actually the tensorflow object detection tutorial notebook(it comes with the tensorflow object detection api)
The complete error log:
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'
Answers:
The protoc version I got through pip show protobuf
and protoc --version
were different. The version in pip was a bit outdated.
After I upgraded the pip version with
pip install --upgrade protobuf
the problem was solved.
These three commands solved it for me:
pip uninstall protobuf python3-protobuf
pip install --upgrade pip
pip install --upgrade protobuf
After trying many different solutions (i’m working on a Mac) the one that worked for me is to reinstall protobuf using:
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
As is highlighted in this article
For others looking into this more recently newer google libraries are using proto plus, a wrapper for python proto messages. Using this helper function worked for me (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))
the problem for me was i had libprotobuf
at 3.11
and protobuf
at 3.15
upgrading libprotobuf fixed it for me.
This can be due to incorrect versions of protobuf and python3-protobuf in your IDE(in my case PyCharm) and in site-packages. Following solved error in my case:
pip uninstall python3-protobuf
pip uninstall protobuf
then finally do
pip install protobuf
Also if you have requirements.txt or setup.py check protobuf version from pip freeze and use the same version in your requirements.txt file
pip freeze
(check protobuf version, and use same in requirements.txt)
protobuf==3.15.6
(in my case pip freeze gave me this version for protobuf)
I got this problem in Anaconda too.
I installed it with:
conda install -c conda-forge streamlit
with the versions protobuf
and libprotobuf
of 3.11.4
I was not able to update neither protobuf
nor libprotobuf
.
I recommend to install first "conda install protobuf" and then
conda install -c conda-forge streamlit
I got now the versions protobuf
and libprotobuf
of 3.14.0 and it works.
This can also happen if you use protoc to generate your language specific bindings file in a different OS than where you import it. For example,
Linux OS
protoc -I=./ --python_out=./ my_module.proto
Mac OS
import my_module_pb2
The issue of my condition is protoc
not install.
step to solve the problem:
- check
pip3 show protobuf
, you will see version of protobuf
.
- e.q.
Version: 3.17.3
- goto protobuf release page and install same version of
protoc
- e.q.
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip
unzip protoc-3.17.3-linux-x86_64.zip
vim ~/.bashprofile
and paste PATH=$PATH:/home/YOUR_HOST_NAME/bin
source ~/.bashprofile
and echo $PATH
to check whether /home/YOUR_HOST_NAME/bin
exist or not.
- check
pip3 show protobuf
is equal to protoc --version
- If not work, reboot and try again.
I encountered it while executing from object_detection.utils import label_map_util
in jupyter notebook. It is actually the tensorflow object detection tutorial notebook(it comes with the tensorflow object detection api)
The complete error log:
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'
The protoc version I got through pip show protobuf
and protoc --version
were different. The version in pip was a bit outdated.
After I upgraded the pip version with
pip install --upgrade protobuf
the problem was solved.
These three commands solved it for me:
pip uninstall protobuf python3-protobuf
pip install --upgrade pip
pip install --upgrade protobuf
After trying many different solutions (i’m working on a Mac) the one that worked for me is to reinstall protobuf using:
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
As is highlighted in this article
For others looking into this more recently newer google libraries are using proto plus, a wrapper for python proto messages. Using this helper function worked for me (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))
the problem for me was i had libprotobuf
at 3.11
and protobuf
at 3.15
upgrading libprotobuf fixed it for me.
This can be due to incorrect versions of protobuf and python3-protobuf in your IDE(in my case PyCharm) and in site-packages. Following solved error in my case:
pip uninstall python3-protobuf
pip uninstall protobuf
then finally do
pip install protobuf
Also if you have requirements.txt or setup.py check protobuf version from pip freeze and use the same version in your requirements.txt file
pip freeze
(check protobuf version, and use same in requirements.txt)
protobuf==3.15.6
(in my case pip freeze gave me this version for protobuf)
I got this problem in Anaconda too.
I installed it with:
conda install -c conda-forge streamlit
with the versions protobuf
and libprotobuf
of 3.11.4
I was not able to update neither protobuf
nor libprotobuf
.
I recommend to install first "conda install protobuf" and then
conda install -c conda-forge streamlit
I got now the versions protobuf
and libprotobuf
of 3.14.0 and it works.
This can also happen if you use protoc to generate your language specific bindings file in a different OS than where you import it. For example,
Linux OS
protoc -I=./ --python_out=./ my_module.proto
Mac OS
import my_module_pb2
The issue of my condition is protoc
not install.
step to solve the problem:
- check
pip3 show protobuf
, you will see version ofprotobuf
.- e.q.
Version: 3.17.3
- e.q.
- goto protobuf release page and install same version of
protoc
- e.q.
wget https://github.com/protocolbuffers/protobuf/releases/download/v3.17.3/protoc-3.17.3-linux-x86_64.zip
unzip protoc-3.17.3-linux-x86_64.zip
vim ~/.bashprofile
and pastePATH=$PATH:/home/YOUR_HOST_NAME/bin
source ~/.bashprofile
andecho $PATH
to check whether/home/YOUR_HOST_NAME/bin
exist or not.
- e.q.
- check
pip3 show protobuf
is equal toprotoc --version
- If not work, reboot and try again.