我正在关注使用 Python 的 protobuf 教程(没有用于 JavaScript 的教程)。它不起作用......而且我认为它proto2
作为 Python 2 程序可能已经过时了。如何让它发挥作用?
所以我开始创建一个文件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;
}
然后我
protoc
在 Mac 上安装然后我创建了两个文件夹
In
和Out
,并移动address.proto
到In
,并运行:
protoc -I=In --python_out=Out In/address.proto
然后创建了一个文件:Out/address_pb2.py
,我去了Out
,并添加了文件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()
然后我安装并运行:
pip3 install protobuf --user
pip3 install google --user
pip3 install google-cloud --user
python3 run.py addr.dat
看起来我必须将代码转换为run.py
从print 123
到,print(123)
因为它是 Python3,而不是 Python2。它给了:
Traceback (most recent call last):
File "run.py", line 40, in <module>
address_book = addressbook_pb2.AddressBook()
NameError: name 'addressbook_pb2' is not defined
我还将文件复制addressbook_pb2.py
到foo.py
,然后改用import foo
它,它给出了:
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.
如何让它发挥作用?