Timestamp class missing in google.protobuf.timestamp_pb2 Python
Question:
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'nx1fgoogle/protobuf/timestamp.protox12x0fgoogle.protobuf"+ntTimestampx12x0fnx07secondsx18x01 x01(x03x12rnx05nanosx18x02 x01(x05x42x85x01nx13x63om.google.protobufBx0eTimestampProtoPx01Z2google.golang.org/protobuf/types/known/timestamppbxf8x01x01xa2x02x03GPBxaax02x1eGoogle.Protobuf.WellKnownTypesbx06proto3')
_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 23com.google.protobufB 16TimestampProtoP 01Z2google.golang.org/protobuf/types/known/timestamppb370 01 01242 02 03GPB252 02 36Google.Protobuf.WellKnownTypes'
_TIMESTAMP._serialized_start=52
_TIMESTAMP._serialized_end=95
What should I do?
Thanks so much!
Answers:
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)
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'nx1fgoogle/protobuf/timestamp.protox12x0fgoogle.protobuf"+ntTimestampx12x0fnx07secondsx18x01 x01(x03x12rnx05nanosx18x02 x01(x05x42x85x01nx13x63om.google.protobufBx0eTimestampProtoPx01Z2google.golang.org/protobuf/types/known/timestamppbxf8x01x01xa2x02x03GPBxaax02x1eGoogle.Protobuf.WellKnownTypesbx06proto3')
_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 23com.google.protobufB 16TimestampProtoP 01Z2google.golang.org/protobuf/types/known/timestamppb370 01 01242 02 03GPB252 02 36Google.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)