invoking grpc_toools.protoc inside python script for returns import file not found errors

189 views
Skip to first unread message

ma...@andrey-belyaevskiy.ru

unread,
Jul 27, 2017, 4:00:29 AM7/27/17
to grpc.io
Hi!

I need create script (python) to build python classes from set of proto files.

I found example here grpc_tools/command.py#L35

I took the code
from grpc_tools import protoc 
...
for file_name in proto_file_names:
    file_path = os.path.join(proto_dir, file_name)
    check_path_exist(file_path)
    proto_files.append(file_path)

for proto_file in proto_files:
    command = [
                  'grpc_tools.protoc',
                  '--proto_path={}'.format(proto_path),
                  '--python_out={}'.format(current_directory),
                  '--grpc_python_out={}'.format(current_directory),
              ] + [proto_file]
    if protoc.main(command) != 0:
        sys.stderr.write('warning: {} failed\n'.format(command))


and get following errors while run script (I have a lot of such)
google/protobuf/descriptor.proto: File not found.
full/path/to/my/file.proto: Import "google/protobuf/descriptor.proto" was not found or had errors.
full/path/to/my/file.proto:45:8: "google.protobuf.EnumValueOptions" is not defined.
full/path/to/my/file.proto: "google.protobuf.EnumValueOptions" is not defined.
full/path/to/my/file.proto: "google.protobuf.EnumValueOptions" is not defined.
full/path/to/my/file.proto:51:8: "google.protobuf.EnumOptions" is not defined.

But when I run command line
python -m grpc_tools.protoc -I../../.. --python_out=. --grpc_python_out=. path/to/my/files/*.proto

All is ok. There are no any errors.

Same issue appears for any single proto file (not full set) containing some imports.

I think it somehow related to import paths but I can not find way to fix it. All parameters and current working directory for both (command line and python script) ways are the same.

ma...@andrey-belyaevskiy.ru

unread,
Jul 27, 2017, 4:22:47 AM7/27/17
to grpc.io, ma...@andrey-belyaevskiy.ru
Looks, like adding one more path '--proto_path={}'.format(os.path.join(proto_path, 'contrib/libs/protobuf')) to command fix my problem

четверг, 27 июля 2017 г., 11:00:29 UTC+3 пользователь ma...@andrey-belyaevskiy.ru написал:

Nathaniel Manista

unread,
Jul 27, 2017, 3:58:09 PM7/27/17
to Andrey Belyaevskiy, grpc.io
On Thu, Jul 27, 2017 at 1:22 AM, <ma...@andrey-belyaevskiy.ru> wrote:
Looks, like adding one more path '--proto_path={}'.format(os.path.join(proto_path, 'contrib/libs/protobuf')) to command fix my problem

Glad to hear that you're moving forward; thank you for following up.
-N
Reply all
Reply to author
Forward
0 new messages