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
packageandimportpaths whereprotocis 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.commonshould be apackagename given your use ofimport common/foo.proto.protoc, you need to be very precise with import paths (root folder of your protos i.e.c:/tmp/proto) and proto files (someproto_pathplus path down to proto). For exampleprotoc --proto_path=c:/tmp/proto --python_out=${SOMEWHERE} --pyi_out=${SOMEWHERE} c:/tmp/proto/common/client_accounts.proto. Theproto_pathgets us to the root folder of the protos and then the remaining path includes thepackagename (path) and ends in the proto.