gpt4 book ai didi

python - 为什么我在使用 Google Protocol Buffers 时会看到 "cannot import name descriptor_pb2"错误?

转载 作者:太空狗 更新时间:2023-10-29 21:20:30 44 4
gpt4 key购买 nike

当使用我们的 protobuf 类生成的 Python 代码时,我们得到这个错误:

cannot import name descriptor_pb2

等效的 C++ 生成的代码工作得很好,所以看起来我们的实际原型(prototype)定义没有问题。

当我尝试导入我们的类时出现此错误,如下所示:

import sys
sys.path.append('..\path\to\generated')
sys.path.append('..\contrib\protobuf\python')

from foobar_pb2 import FooBar

附加系统路径是否正确?

我在 protobuf\python\google\protobuf 目录中检查了 descriptor_pb2.py 但只找到了 descriptor.py - 我们使用最新版本,所以我假设我们没有丢失任何文件。

有谁知道解决办法是什么?

最佳答案

我相信你必须自己用 protoc 生成 descriptor_pb2.py:

protoc descriptor.proto --python_out=gen/

gen/ 是一个包含生成的 python 类的文件夹。

之后,下面的工作正常:

sys.path.append('../gen')
from descriptor_pb2 import FileDescriptorSet

../gen/descriptor_pb2.py 必须存在。

关于python - 为什么我在使用 Google Protocol Buffers 时会看到 "cannot import name descriptor_pb2"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1828379/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com