How to make a basic protobuf program work using Python as on Google's Developer's Website?

571 views Asked by At

I am following the tutorial of protobuf using Python (there isn't one for JavaScript). It doesn't work... and I think it might be outdated as proto2 and as a Python 2 program. How to make it work?

So I started with creating a file address.proto:

syntax = "proto2";

package tutorial;

message Person {
  optional string name = 1;
  optional int32 id = 2;
  optional string email = 3;

  enum PhoneType {
    MOBILE = 0;
    HOME = 1;
    WORK = 2;
  }

  message PhoneNumber {
    optional string number = 1;
    optional PhoneType type = 2 [default = HOME];
  }

  repeated PhoneNumber phones = 4;
}

message AddressBook {
  repeated Person people = 1;
}
  1. And then I installed protoc on the Mac

  2. And then I created two folders In and Out, and moved address.proto into In, and run:

protoc -I=In --python_out=Out In/address.proto 

and then there is a file created: Out/address_pb2.py, and I went to Out, and added the file run.py:

#! /usr/bin/python

import addressbook_pb2
import sys

# This function fills in a Person message based on user input.
def PromptForAddress(person):
  person.id = int(raw_input("Enter person ID number: "))
  person.name = raw_input("Enter name: ")

  email = raw_input("Enter email address (blank for none): ")
  if email != "":
    person.email = email

  while True:
    number = raw_input("Enter a phone number (or leave blank to finish): ")
    if number == "":
      break

    phone_number = person.phones.add()
    phone_number.number = number

    type = raw_input("Is this a mobile, home, or work phone? ")
    if type == "mobile":
      phone_number.type = addressbook_pb2.Person.PhoneType.MOBILE
    elif type == "home":
      phone_number.type = addressbook_pb2.Person.PhoneType.HOME
    elif type == "work":
      phone_number.type = addressbook_pb2.Person.PhoneType.WORK
    else:
      print "Unknown phone type; leaving as default value."

# Main procedure:  Reads the entire address book from a file,
#   adds one person based on user input, then writes it back out to the same
#   file.
if len(sys.argv) != 2:
  print "Usage:", sys.argv[0], "ADDRESS_BOOK_FILE"
  sys.exit(-1)

address_book = addressbook_pb2.AddressBook()

# Read the existing address book.
try:
  f = open(sys.argv[1], "rb")
  address_book.ParseFromString(f.read())
  f.close()
except IOError:
  print sys.argv[1] + ": Could not open file.  Creating a new one."

# Add an address.
PromptForAddress(address_book.people.add())

# Write the new address book back to disk.
f = open(sys.argv[1], "wb")
f.write(address_book.SerializeToString())
f.close()

and then I installed and ran:

pip3 install protobuf --user
pip3 install google --user
pip3 install google-cloud --user

python3 run.py addr.dat

and it looks like I have to convert the code in run.py from print 123 to print(123) because it is Python3, not Python2. And it gave:

Traceback (most recent call last):
  File "run.py", line 40, in <module>
    address_book = addressbook_pb2.AddressBook()
NameError: name 'addressbook_pb2' is not defined

I also copied the file addressbook_pb2.py to foo.py, and then use import foo instead, and it gave:

Traceback (most recent call last):
  File "run.py", line 3, in <module>
    import foo
  File "/Users/peter/code/TryProtobuf_Unzipped/TryIt/Out/foo.py", line 34, in <module>
    _descriptor.EnumValueDescriptor(
  File "/Users/peter/Library/Python/3.8/lib/python/site-packages/google/protobuf/descriptor.py", line 732, in __new__
    _message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors should not be created directly, but only retrieved from their parent.

How can it be made to work?

1

There are 1 answers

0
sfphoton On

Your address.proto declares the tutorial package. I think you can access it via address_book = tutorial.addressbook_pb2.AddressBook()

As for why it broke after renaming the file to foo.py, I have no idea.