Search code examples
pythonprotocol-buffersimporterrorgtfs

Having problems using the python code generated from the google protocol buffer


I am getting the following error when I try to run gtfs_realtime_pb2.py (the python code generated from running the gtfs-realtime.proto through google's protocol buffer):

Traceback (most recent call last):
  File "<pyshell#1>", line 1, in <module>
    from google.protobuf import descriptor
ImportError: No module named 'google'

This is a link to the specific code I am having trouble with: https://github.com/mattwigway/gtfsrdb

And the link to installing google's protocol buffer:
https://developers.google.com/protocol-buffers/

I am pretty certain I have install the buffer correctly, so why is it throwing up that it can't find this module?


Solution

  • Included in protocol buffer is descriptor.proto; python code for this proto should of been generated as part of the install process.

    The protocol buffer you are using uses descriptor.proto so the directory where it resides needs to be included via the --proto_path= parameter (see Importing "google/protobuf/descriptor.proto" in java protocol buffers

    For window systems, the install directory will be where you placed it; for *nix it will probably be answer taken from

        /usr/include/descriptor.proto 
    or 
        /usr/local/include/descriptor.proto 
    

    i.e. for java you would do something like

    protoc addressbookSD.proto --java_out=./ --proto_path=./ --proto_path=<protobuf-install-directory>/src
    

    where <protobuf-install-directory> is the protocol buffers install directory. The key point is descriptor.proto is in

    <protobuf-install-directory>\src\google\protobuf