views:

224

answers:

2

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?

+1  A: 

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

IggShaman
A: 

Please make sure to install the protobuf runtime libary 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 as part of the installation process.

Kenton Varda