2

I have been using .proto files without any issue with C# and Java. I have more than hundred proto files spread across a folder hierarchy. I can compile in Visual Studio and in JetBrains IntelliJ Idea. Now I also want to compile in python. The files use a lot of import statements. There are no conflicts in this (no cyclic imports)...works fine in C# and Java. I'm not able to compile in python. My folder structure is simple:

c:\\tmp\\proto

Under this there are many folders containing proto files. I'm currently only using 'common' folder: c:\tmp\proto\common

Following cmd line works fine:

protoc --proto_path=c:/tmp/proto/common --python_out=c:/tmp/generated_python_files c:/tmp/proto/common\\app_codes.proto

This file has no imports. I get the py file in the output folder. Following fails:

protoc --proto_path=c:/tmp/proto/common --python_out=c:/tmp/generated_python_files c:/tmp/proto/common\\client_accounts.proto

This file uses imports, as shown:

import "google/protobuf/timestamp.proto";
import "common/environment_params.proto"; 
import "common/status_main.proto";
import "common/aws_account.proto";
import "common/azure_account.proto";
import "common/google_account.proto";

Result in cmd:

C:\tmp\proto>protoc --proto_path=c:/tmp/proto/common/ --python_out=c:/tmp/generated_python_files client_accounts.proto

common/environment_params.proto: File not found.
common/status_main.proto: File not found.
common/aws_account.proto: File not found.
common/azure_account.proto: File not found.
common/google_account.proto: File not found.
c:/tmp/proto/common//client_accounts.proto:8:1: Import "common/environment_params.proto" was not found or had errors.
c:/tmp/proto/common//client_accounts.proto:9:1: Import "common/status_main.proto" was not found or had errors.
c:/tmp/proto/common//client_accounts.proto:10:1: Import "common/aws_account.proto" was not found or had errors.
c:/tmp/proto/common//client_accounts.proto:11:1: Import "common/azure_account.proto" was not found or had errors.
c:/tmp/proto/common//client_accounts.proto:12:1: Import "common/google_account.proto" was not found or had errors.
c:/tmp/proto/common//client_accounts.proto:43:3: "EnvironmentParams" is not defined.
c:/tmp/proto/common//client_accounts.proto:51:3: "Status" is not defined.
c:/tmp/proto/common//client_accounts.proto:61:3: "EnvironmentParams" is not defined.
c:/tmp/proto/common//client_accounts.proto:75:3: "EnvironmentParams" is not defined.
c:/tmp/proto/common//client_accounts.proto:94:3: "AzureAccount" is not defined.
c:/tmp/proto/common//client_accounts.proto:98:3: "AwsAccount" is not defined.
c:/tmp/proto/common//client_accounts.proto:102:3: "GoogleAccount" is not defined.
c:/tmp/proto/common//client_accounts.proto:27:60: "Status" is not defined.
c:/tmp/proto/common//client_accounts.proto:30:64: "Status" is not defined.

I have tried running the protoc command from various folders: c:/tmp or c:/tmp/proto or c:/tmp/proto/common. Same error. I have tried setting --proto_path to c:/tmp/proto and c:/tmp.

I have also tried using the 'grpc compiler' extension in VS Code. It works for files without imports. Fails otherwise. Same error as above. I set the --proto_path in settings.json. Does not work.

I believe somewhere path is not being passed properly. Issue is not in proto file imports - it compiles with c# and java.

What could be the issue?

regards Vinay

2
  • Your question is not a minimal repro making it more difficult to help. I suspect the IDEs are helping you with package and import paths where protoc is notoriously gnarly, prickly and difficult to use. I created a minimal repro of your issue and am able to generate python sources. I think (!?) your issue is that you're not correctly representing protobuf packages. common should be a package name given your use of import common/foo.proto. Commented Sep 9 at 21:58
  • With protoc, you need to be very precise with import paths (root folder of your protos i.e. c:/tmp/proto) and proto files (some proto_path plus path down to proto). For example protoc --proto_path=c:/tmp/proto --python_out=${SOMEWHERE} --pyi_out=${SOMEWHERE} c:/tmp/proto/common/client_accounts.proto. The proto_path gets us to the root folder of the protos and then the remaining path includes the package name (path) and ends in the proto. Commented Sep 9 at 22:00

2 Answers 2

2

A minimal-repro:

NOTE Linux not Windows user.

.
└── proto
    └── common
        ├── client_accounts.proto
        └── google_account.proto

client_accounts.proto:

syntax = "proto3";

package common;

import "google/protobuf/timestamp.proto";
import "common/google_account.proto";

message ClientAccount {
    google.protobuf.Timestamp last_login = 1;
    common.GoogleAccount google_account = 2;
}

google_account.proto:

syntax = "proto3";

package common;

message GoogleAccount {}

And:

PROTOS="${PWD}/proto"
PYTHON="${PWD}/gen"

protoc \
--proto_path=${PROTOS} \
--python_out=${PYTHON} \
--pyi_out=${PYTHON} \
${PROTOS}/common/client_accounts.proto

Yields:

.
├── gen
│   └── common
│       ├── client_accounts_pb2.py
│       └── client_accounts_pb2.pyi
└── proto
    └── common
        ├── client_accounts.proto
        └── google_account.proto
Sign up to request clarification or add additional context in comments.

2 Comments

DazWilkin ...Please see my post below
I'm glad to see you resolved the issue!
1

I am finally able to get this to work. Changed the --proto_path to point to the root of all proto folder hierarchy. I got it working in the morning. Just now saw your solution. I will accept it - you are right. I thought it would be some such silly mistake in paths. Yesterday I was lost in VS Code, Visual Studio and PyCharm.

C:\tmp\proto>python -m grpc_tools.protoc --proto_path=c:/tmp/proto/ --python_out=c:/tmp/generated_python_files --grpc_python_out=c:/tmp/generated_python_files c:/tmp/proto/common/*.proto

I also changed to grpc_tools.protoc to generate the client and server stubs. And now *.proto also works. Able to generate for all my files (one folder at a time).

Thanks

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.