作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当使用我们的 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/
我是一名优秀的程序员,十分优秀!