简体   繁体   中英

Timestamp class missing in google.protobuf.timestamp_pb2 Python

I'm working with protobuf 3.20.1 in Python. However when I tried to use Timestamp, this error showed up:

from google.protobuf import timestamp_pb2
timestamp = timestamp_pb2.Timestamp()

ERROR: Cannot find reference 'Timestamp' in 'timestamp_pb2.py'

Here is what I found in timestamp.proto

syntax = "proto3";

package google.protobuf;

option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option cc_enable_arenas = true;
option go_package = "github.com/golang/protobuf/ptypes/timestamp";
option java_package = "com.google.protobuf";
option java_outer_classname = "TimestampProto";
option java_multiple_files = true;
option objc_class_prefix = "GPB";

message Timestamp {
  int64 seconds = 1;
  int32 nanos = 2;
}

And here is what's inside timestamp_pb2.py. There is no Timestamp class.

from google.protobuf.internal import builder as _builder
from google.protobuf import descriptor as _descriptor
from google.protobuf import descriptor_pool as _descriptor_pool
from google.protobuf import symbol_database as _symbol_database
# @@protoc_insertion_point(imports)

_sym_db = _symbol_database.Default()




DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x1fgoogle/protobuf/timestamp.proto\x12\x0fgoogle.protobuf\"+\n\tTimestamp\x12\x0f\n\x07seconds\x18\x01 \x01(\x03\x12\r\n\x05nanos\x18\x02 \x01(\x05\x42\x85\x01\n\x13\x63om.google.protobufB\x0eTimestampProtoP\x01Z2google.golang.org/protobuf/types/known/timestamppb\xf8\x01\x01\xa2\x02\x03GPB\xaa\x02\x1eGoogle.Protobuf.WellKnownTypesb\x06proto3')

_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, globals())
_builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'google.protobuf.timestamp_pb2', globals())
if _descriptor._USE_C_DESCRIPTORS == False:

  DESCRIPTOR._options = None
  DESCRIPTOR._serialized_options = b'\n\023com.google.protobufB\016TimestampProtoP\001Z2google.golang.org/protobuf/types/known/timestamppb\370\001\001\242\002\003GPB\252\002\036Google.Protobuf.WellKnownTypes'
  _TIMESTAMP._serialized_start=52
  _TIMESTAMP._serialized_end=95

What should I do?

Thanks so much!

Your environment is likely misconfigured.

Python's implementation of protobuf is confusing because it relies on metaclasses.

timestamp_pb2.py is correct as your describe.

python3 -m venv venv
source venv/bin/activate

python3 -m pip install protobuf==3.20.1

Then your code should work:

main.py :

from google.protobuf import timestamp_pb2


timestamp = timestamp_pb2.Timestamp()
print(type(timestamp))

timestamp.GetCurrentTime()
print(timestamp)

Yields, for example:

<class 'google.protobuf.timestamp_pb2.Timestamp'>
seconds: 1672376848
nanos: 270336000

Protobuf 3.20.1 is old (21 April 2022) and predates some significant changes that were made to the Python implementation in May 2022 (see link )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM