Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers?

When using the generated Python code from our protobuf classes, we get this error:

cannot import name descriptor_pb2

The equivalent C++ generated code works just fine, so it would appear that there is no problem with our actual proto definitions.

This error occurs when I try and import our class, like so:

import sys
sys.path.append('..\path\to\generated')
sys.path.append('..\contrib\protobuf\python')

from foobar_pb2 import FooBar

Is it correct to append the system paths?

I checked in the protobuf\python\google\protobuf directory for descriptor_pb2.py but only found descriptor.py - we're using the latest version, so I assume we don't have any files missing.

Does anyone know what the solution is?

like image 868
Nick Bolton Avatar asked Dec 01 '09 19:12

Nick Bolton


People also ask

Does Google use Protobuf?

Protocol buffers are Google's language-neutral, platform-neutral, extensible mechanism for serializing structured data – think XML, but smaller, faster, and simpler.

What is Google Protobuf message?

namespace google::protobuf. Defines Message, the abstract interface implemented by non-lite protocol message objects. Although it's possible to implement this interface manually, most users will use the protocol compiler to generate implementations.

What is Google Protobuf value?

Value represents a dynamically typed value which can be either null, a number, a string, a boolean, a recursive struct value, or a list of values. A producer of value is expected to set one of that variants, absence of any variant indicates an error.

What is Protobuf descriptor?

descriptor. Descriptors essentially contain exactly the information found in a . proto file, in types that make this information accessible in Python.


4 Answers

I believe you have to generate descriptor_pb2.py with protoc yourself:

protoc descriptor.proto --python_out=gen/

gen/ is a folder with generated python classes.

After that, the following works just fine:

sys.path.append('../gen')
from descriptor_pb2 import FileDescriptorSet

../gen/descriptor_pb2.py must exists.

like image 72
IggShaman Avatar answered Oct 15 '22 07:10

IggShaman


In my case, not finding descriptor_pb2 occurred because protobuf wasn't correctly installed. In the python subdirectory of protobuf, be sure to run

python setup.py build
python setup.py test
python setup.py install (as root)
like image 32
user1381 Avatar answered Oct 15 '22 06:10

user1381


Please make sure to install the protobuf runtime library as directed in the readme file. You cannot simply use the source directly out of the package, since descriptor_pb2.py needs to be generated by protoc (the protobuf compiler) as part of the installation process.

like image 21
Kenton Varda Avatar answered Oct 15 '22 08:10

Kenton Varda


I use python 2.7 on windows 10.

In my case, I have downloaded protoc-3.0.0-beta-2-win32 from https://github.com/google/protobuf/releases and copied the binary protoc file to src folder.

after that I have run the command python setup.py build and the descriptor_pb2 was generated.

like image 23
Mehmet Ali Avatar answered Oct 15 '22 08:10

Mehmet Ali