具有以下结构:
- project1
- project1.py
- protofile_pb2.py
- protofile_pb2_grpc.py
- project2
- project2.py
- protofile_pb2.py
- protofile_pb2_grpc.py
project1.py:
import protofile_pb2.py
...
project2.py:
import protofile_pb2
import project1
...
运行project2.py时,出现此错误:
TypeError: Couldn't build proto file into descriptor pool!
Invalid proto descriptor for file "protofile.proto":
protofile.proto: A file with this name is already in the pool.
答案 0 :(得分:2)
您正在使用生成的protofile.proto的不同版本。确保重新生成protobuf文件,它应该可以正常工作。
答案 1 :(得分:0)
根据this comment,它对我有用:
pip uninstall protobuf
pip install --no-binary protobuf protobuf
答案 2 :(得分:0)
在Python中部署Google云功能时遇到了类似的问题。我的云功能在我的模块中使用了不同版本的.proto文件(这又是一个我想使用Requirements.txt安装的私有模块)。解决方案是使用纯python实现,而不是使用protobuf的二进制实现。要在云函数或云构建中解决此问题,您可以将云函数的env变量设置为使用协议缓冲区的python实现。
在您的gcloud cloud功能deploy命令中,使用以下选项:
index.py
或者如果您在任何其他环境中,只需执行此操作
<start of command> --set-env-vars=PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION='python' <your rest of the command>
这解决了我的问题。