如何使用生成的python protobuf文件?

时间:2018-10-26 22:21:26

标签: python protocol-buffers

按照https://developers.google.com/protocol-buffers/docs/pythontutorial上的教程进行操作时,我遇到了第一个问题。我只想初始化一个非常简单的枚举。这是我的消息(UserRole.proto):

syntax = "proto3";

package benji;

enum UserRole {
    superUser = 0;
    admin = 1;
    recruiter = 2;
    regular = 3;
}

这就是我要在MyFile.py中使用它的方式:

import UserRole_pb2

myUserRole = UserRole_pb2().UserRole()

其他信息:

enter image description here

这是生成的文件(UserRole_pb2.py):

# Generated by the protocol buffer compiler.  DO NOT EDIT!
# source: UserRole.proto

import sys
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
from google.protobuf.internal import enum_type_wrapper
from google.protobuf import descriptor as _descriptor
from google.protobuf import message as _message
from google.protobuf import reflection as _reflection
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor.FileDescriptor(
  name='UserRole.proto',
  package='benji',
  syntax='proto3',
  serialized_options=None,
  serialized_pb=_b('\n\x0eUserRole.proto\x12\x05\x62\x65nji*@\n\x08UserRole\x12\r\n\tsuperUser\x10\x00\x12\t\n\x05\x61\x64min\x10\x01\x12\r\n\trecruiter\x10\x02\x12\x0b\n\x07regular\x10\x03\x62\x06proto3')
)

_USERROLE = _descriptor.EnumDescriptor(
  name='UserRole',
  full_name='benji.UserRole',
  filename=None,
  file=DESCRIPTOR,
  values=[
    _descriptor.EnumValueDescriptor(
      name='superUser', index=0, number=0,
      serialized_options=None,
      type=None),
    _descriptor.EnumValueDescriptor(
      name='admin', index=1, number=1,
      serialized_options=None,
      type=None),
    _descriptor.EnumValueDescriptor(
      name='recruiter', index=2, number=2,
      serialized_options=None,
      type=None),
    _descriptor.EnumValueDescriptor(
      name='regular', index=3, number=3,
      serialized_options=None,
      type=None),
  ],
  containing_type=None,
  serialized_options=None,
  serialized_start=25,
  serialized_end=89,
)
_sym_db.RegisterEnumDescriptor(_USERROLE)

UserRole = enum_type_wrapper.EnumTypeWrapper(_USERROLE)
superUser = 0
admin = 1
recruiter = 2
regular = 3


DESCRIPTOR.enum_types_by_name['UserRole'] = _USERROLE
_sym_db.RegisterFileDescriptor(DESCRIPTOR)


# @@protoc_insertion_point(module_scope)

好吧,它在生成的文件的第4行附近失败。这行:

from google.protobuf.internal import enum_type_wrapper

它抛出“任何异常”。 如何在Python中使用这个简单的生成文件?

0 个答案:

没有答案