when use python protobuf3 to import Timestamp, get except " KeyError: 'google'"

749 views
Skip to first unread message

Delvin Pan

unread,
Dec 24, 2017, 11:18:29 PM12/24/17
to Protocol Buffers
Hi All,

I have been using protobuf3 for a while and it works most of time, until I try to use package google.protobuf.timestamp.

Here is the problems:

os : ubuntu 16.04

protoc installment : follwing the instruments in  https://gist.github.com/sofyanhadia/37787e5ed098c97919b8c593f0ec44d8

python protobuf3 installment:  sudo pip3 install protobuf3

test code: protobuftest.proto  in the attachment

cmd to run: 

protoc -I=.  --python3_out=. protobuftest.proto

Result: 

Traceback (most recent call last):
  File "/usr/local/bin/protoc-gen-python3", line 36, in <module>
    compiler = Compiler(desc_map[file_name], top_level_elements)
  File "/usr/local/lib/python3.5/dist-packages/protobuf3/compiler/__init__.py", line 54, in __init__
    self.process_message(message)
  File "/usr/local/lib/python3.5/dist-packages/protobuf3/compiler/__init__.py", line 104, in process_message
    self.process_field(message.name, field, embedded)
  File "/usr/local/lib/python3.5/dist-packages/protobuf3/compiler/__init__.py", line 148, in process_field
    file_to_import = splitext(self.__tle_map[top_level_name])[0].replace('/', '.')
KeyError: 'google'
--python3_out: protoc-gen-python3: Plugin failed with status code 1.

Thanks a lot for your help.

best
Delvin




protobuftest.proto

Delvin Pan

unread,
Dec 24, 2017, 11:27:40 PM12/24/17
to Protocol Buffers
I have debug the code in "/usr/local/lib/python3.5/dist-packages/protobuf3/compiler/__init__.py" and here is what I have found:

(1)  in "file_to_import = splitext(self.__tle_map[top_level_name])[0].replace('/', '.')"

top_level_name is "google"

self.__tle_map is 
[INFO] 2017-12-25 12:22:59,476 MainThread __init__.py 149 tle_map: {'Any': 'google/protobuf/any.proto', 'ErrorStatus': 'protobuftest.proto'}

obviously, top_level_name is "Any" will be the correct answer. 
In the protobuftest.proto,  the message is "google.protobuf.Any" and the first word "google" is taken as top_level_name.
It seems, there should be a lack of strip to the package names .

Not quite sure. Hope any one is interested in this problem.

Merry Christmas and Happy New Year !

best
Delvin 

Delvin Pan

unread,
Dec 24, 2017, 11:31:16 PM12/24/17
to Protocol Buffers
I have also tried to remove google.protobuf package path 

syntax = "proto3";
import "google/protobuf/any.proto";

message ErrorStatus {
  string message = 1;
  google.protobuf.Any details = 2;  --> changed to "Any details = 2;"  
}

And get the following errors:

protoc -I=.  --python3_out=python/ protobuftest.proto
protobuftest.proto:6:3: "Any" is not defined.
protobuftest.proto: warning: Import google/protobuf/any.proto but not used.

Josh Haberman

unread,
Jan 8, 2018, 1:21:03 PM1/8/18
to Protocol Buffers
protobuf3 is a separate package, unrelated to the Google release of protobuf.

The regular Google release of protobuf python (--python_out=.) should work with Python 3.
Reply all
Reply to author
Forward
0 new messages