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

82,224

Solution 1

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.

Solution 2

These three commands solved it for me:

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

Solution 3

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)

Solution 4

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))

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

Solution 5

the problem for me was i had libprotobuf at 3.11 and protobuf at 3.15 upgrading libprotobuf fixed it for me.

Share:
82,224
Reghunaath A A
Author by

Reghunaath A A

Updated on August 17, 2021

Comments

  • Reghunaath A A
    Reghunaath A A over 2 years

    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
    
    ~\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'
    
  • alejandro
    alejandro over 3 years
    In my case, I only had to upgrade pip and and protobuf (within the virtual environment) from 3.11.3 to 3.13.0
  • PolyTekPatrick
    PolyTekPatrick over 3 years
    Simply updating protobuf from 3.11.2 to 3.13.0 was sufficient for me.
  • Khanchi97
    Khanchi97 over 3 years
    If python3, use pip3 install --upgrade protobuf.
  • Mutlu Simsek
    Mutlu Simsek about 3 years
    There is no package called libprotobuf on pypi?
  • davzaman
    davzaman about 3 years
    I think it's on anaconda, I didn't explicitly install it I think it had gotten installed as a dependency of something else.
  • user8491363
    user8491363 almost 3 years
    This worked for me. Specifically, by doing conda install -c conda-forge libprotobuf=3.15. I had to specify the version and upgrade it.
  • Can
    Can almost 3 years
    It also works for me. Although I upgraded protobuf, it did not work. Then I run the following comment pip uninstall protobuf python3-protobuf and it worked. Thanks!
  • Soren
    Soren about 2 years
    Not working any more.
  • Brent Brewington
    Brent Brewington about 2 years
    I got this to work (on Ubuntu) with: pip install --upgrade pip --> pip install --upgrade protobuf - and not sure how to show as multi-line comment here, hence the -->