文件不在指定的任何路径中

File does not reside within any path specified

我想在当前目录和所有子目录中递归编译protobuf文件。我是运行以下脚本:

D:\Code\cloudapi>for /R %i  in (*) do python -m grpc_tools.protoc --python_out=. --proto_path=. %i

但我收到的唯一信息是以下错误:

D:\Code\cloudapi\third_party\googleapis\google\type\dayofweek.proto: File does n ot reside within any path specified using --proto_path (or -I). You must specif y a --proto_path which encompasses this file. Note that the proto_path must be an exact prefix of the .proto file names -- protoc is too dumb to figure out whe n two paths (e.g. absolute and relative) are equivalent (it's harder than you th ink).

这个问题有什么可能的解决方案吗?

是的,您可以使用 windows FOR 命令的选项来指向文件夹。

像这样的东西应该可以工作:

for /R %i  in (*) do python -m grpc_tools.protoc --python_out= %~di%~pi --proto_path= %~di%~pi %i

我猜您希望输出与源文件夹位于同一文件夹中。

我在这里所做的是使用 Windows FOR 命令的可选扩展语法。如果你去 for /? 你可以看到更多选项。

基本上 %~di 被翻译成 %i 的完整路径的驱动器号,这是您的文件。 %~pi 是该文件夹的路径(不含驱动器)。

所以我在 --proto_path 参数上指定了它,这应该会让它振作起来。我对 --python_out 使用了相同的方法,但您可能希望将其恢复为 . 以使其进入父文件夹。