Protobuf python tutorial error

2,010 views
Skip to first unread message

Junhao Wen

unread,
Oct 26, 2016, 4:21:40 PM10/26/16
to Protocol Buffers
Hello, I followed the official tutorial for python, https://developers.google.com/protocol-buffers/docs/pythontutorial
I run protoc 3.1.0 in my Mac, and in the tutorial, in the section of  

Writing A Message

Now let's try using your protocol buffer classes. The first thing you want your address book application to be able to do is write personal details to your address book file. To do this, you need to create and populate instances of your protocol buffer classes and then write them to an output stream.

Here is a program which reads an AddressBook from a file, adds one new Person to it based on user input, and writes the new AddressBook back out to the file again. The parts which directly call or reference code generated by the protocol compiler are highlighted.


What i understood here for the red line is that It refers the addressbook.proto, I use the tutorial code and run it in terminal with this command:

python test.py addressbook.proto

test.py contains the tutorial code, but i always got the error cause by this line, address_book.ParseFromString(f.read())

Here is the error:
Traceback (most recent call last):
  File "test.py", line 56, in <module>
    address_book.ParseFromString(f.read())
  File "/Users/junhao.wen/anaconda2/lib/python2.7/site-packages/protobuf-3.1.0-py2.7.egg/google/protobuf/message.py", line 185, in ParseFromString
    self.MergeFromString(serialized)
  File "/Users/junhao.wen/anaconda2/lib/python2.7/site-packages/protobuf-3.1.0-py2.7.egg/google/protobuf/internal/python_message.py", line 1087, in MergeFromString
    if self._InternalParse(serialized, 0, length) != length:
  File "/Users/junhao.wen/anaconda2/lib/python2.7/site-packages/protobuf-3.1.0-py2.7.egg/google/protobuf/internal/python_message.py", line 1113, in InternalParse
    new_pos = local_SkipField(buffer, new_pos, end, tag_bytes)
  File "/Users/junhao.wen/anaconda2/lib/python2.7/site-packages/protobuf-3.1.0-py2.7.egg/google/protobuf/internal/decoder.py", line 850, in SkipField
    return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
  File "/Users/junhao.wen/anaconda2/lib/python2.7/site-packages/protobuf-3.1.0-py2.7.egg/google/protobuf/internal/decoder.py", line 799, in _SkipGroup
    new_pos = SkipField(buffer, pos, end, tag_bytes)
  File "/Users/junhao.wen/anaconda2/lib/python2.7/site-packages/protobuf-3.1.0-py2.7.egg/google/protobuf/internal/decoder.py", line 850, in SkipField
    return WIRETYPE_TO_SKIPPER[wire_type](buffer, pos, end)
  File "/Users/junhao.wen/anaconda2/lib/python2.7/site-packages/protobuf-3.1.0-py2.7.egg/google/protobuf/internal/decoder.py", line 820, in _RaiseInvalidWireType
    raise _DecodeError('Tag had invalid wire type.')
google.protobuf.message.DecodeError: Tag had invalid wire type.


Can anybody help me????

Thanks in advance 


ramrun...@gmail.com

unread,
Oct 27, 2016, 1:45:20 PM10/27/16
to Protocol Buffers
You might be slightly confused regarding the workflow here.
the .proto file that you pass as an argument to your test.py program does not belong there.
This file is the protocol buffer specification that you pass to the protocol buffer compiler in order for it to generate the file addressbook_pb2.py
This is described in the section  "Compiling your Protocol Buffers".

Then if you notice  you import that file in your python program therefore giving it the ability to understand how to operate on the types of messages you are describing
in the .proto file.

Now that file will be an addressbook. if you don't have one already (you probably don't) you need to create one via running the add_person.py program
this program accepts an argument of an addressbook file to add a person and if that file is non existent it will create a new one (your case)
so run
python add_person.py abfile
add the desired values and then you'll have a new file named abfile in that directory.
that is what you have to pass as an argument to your test.py
Reply all
Reply to author
Forward
0 new messages