protofile.proto:池中已存在同名文件

Abd*_*aid 3 python file-descriptor protocol-buffers grpc

具有以下结构:

- project1
  - project1.py
  - protofile_pb2.py
  - protofile_pb2_grpc.py
- project2
  - project2.py
  - protofile_pb2.py
  - protofile_pb2_grpc.py
Run Code Online (Sandbox Code Playgroud)

项目1.py:

- project1
  - project1.py
  - protofile_pb2.py
  - protofile_pb2_grpc.py
- project2
  - project2.py
  - protofile_pb2.py
  - protofile_pb2_grpc.py
Run Code Online (Sandbox Code Playgroud)

项目2.py:

import protofile_pb2.py
...
Run Code Online (Sandbox Code Playgroud)

运行 project2.py 时,出现此错误:

import protofile_pb2
import project1
...
Run Code Online (Sandbox Code Playgroud)

Abd*_*aid 5

您正在使用生成的 protofile.proto 的不同版本。确保重新生成 protobuf 文件,它应该可以正常工作。


Abd*_*aid 5

根据此评论,它对我有用:

pip uninstall protobuf
pip install --no-binary protobuf protobuf
Run Code Online (Sandbox Code Playgroud)


Onk*_*kar 5

我在Python中部署谷歌云函数时遇到了类似的问题。我的云功能在我的模块中使用不同版本的 .proto 文件(这又是一个私有模块,我想使用 requests.txt 安装)。解决方案是使用纯Python实现而不是使用protobuf的二进制实现。要在云函数或云构建中解决此问题,您可以设置云函数环境变量以使用协议缓冲区的 python 实现。

在 gcloud 云功能部署命令中使用以下选项:

<start of command> --set-env-vars=PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION='python' <your rest of the command>
Run Code Online (Sandbox Code Playgroud)

或者如果您在任何其他环境/操作系统中,只需执行此操作

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION='python' 
Run Code Online (Sandbox Code Playgroud)

这解决了我的问题。