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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1828379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 23:09:09  来源:igfitidea点击:

Why do I see "cannot import name descriptor_pb2" error when using Google Protocol Buffers?

pythonprotocol-buffers

提问by Nick Bolton

When using the generated Python code from our protobuf classes, we get this error:

使用从我们的 protobuf 类生成的 Python 代码时,我们收到此错误:

cannot import name descriptor_pb2

The equivalent C++ generated code works just fine, so it would appear that there is no problem with our actual proto definitions.

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

This error occurs when I try and import our class, like so:

当我尝试导入我们的类时会发生此错误,如下所示:

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

from foobar_pb2 import FooBar

Is it correct to append the system paths?

附加系统路径是否正确?

I checked in the protobuf\python\google\protobufdirectory for descriptor_pb2.pybut only found descriptor.py- we're using the latest version, so I assume we don't have any files missing.

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

Does anyone know what the solution is?

有谁知道解决方案是什么?

回答by IggShaman

I believe you have to generate descriptor_pb2.pywith protocyourself:

我相信你有产生descriptor_pb2.pyprotoc自己:

protoc descriptor.proto --python_out=gen/

gen/is a folder with generated python classes.

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

After that, the following works just fine:

之后,以下工作就好了:

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

../gen/descriptor_pb2.pymust exists.

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

回答by user1381

In my case, not finding descriptor_pb2 occurred because protobuf wasn't correctly installed. In the python subdirectory of protobuf, be sure to run

在我的例子中,没有找到descriptor_pb2 是因为protobuf 没有正确安装。在protobuf的python子目录,一定要运行

python setup.py build
python setup.py test
python setup.py install (as root)

回答by Kenton Varda

Please make sure to install the protobuf runtime library as directed in the readme file. You cannot simply use the source directly out of the package, since descriptor_pb2.py needs to be generated by protoc (the protobuf compiler) as part of the installation process.

请确保按照自述文件中的指示安装 protobuf 运行时库。您不能简单地直接从包中使用源代码,因为作为安装过程的一部分,descriptor_pb2.py 需要由 protoc(protobuf 编译器)生成。

回答by Mehmet Ali

I use python 2.7 on windows 10.

我在 Windows 10 上使用 python 2.7。

In my case, I have downloaded protoc-3.0.0-beta-2-win32 from https://github.com/google/protobuf/releasesand copied the binary protoc file to src folder.

就我而言,我已从https://github.com/google/protobuf/releases下载 protoc-3.0.0-beta-2-win32并将二进制 protoc 文件复制到 src 文件夹。

after that I have run the command python setup.py buildand the descriptor_pb2 was generated.

之后我运行了命令python setup.py build并生成了descriptor_pb2。

回答by Rahul Singh

python setup.py build

python setup.py 构建

This step is mandatory as it generated some of the source files.

此步骤是强制性的,因为它生成了一些源文件。

Generating google/protobuf/descriptor_pb2.py... Generating google/protobuf/compiler/plugin_pb2.py... Generating google/protobuf/unittest_pb2.py... Generating google/protobuf/unittest_custom_options_pb2.py... Generating google/protobuf/unittest_import_pb2.py... Generating google/protobuf/unittest_import_public_pb2.py... Generating google/protobuf/unittest_mset_pb2.py... Generating google/protobuf/unittest_no_generic_services_pb2.py... Generating google/protobuf/internal/descriptor_pool_test1_pb2.py... Generating google/protobuf/internal/descriptor_pool_test2_pb2.py... Generating google/protobuf/internal/test_bad_identifiers_pb2.py... Generating google/protobuf/internal/missing_enum_values_pb2.py... Generating google/protobuf/internal/more_extensions_pb2.py... Generating google/protobuf/internal/more_extensions_dynamic_pb2.py... Generating google/protobuf/internal/more_messages_pb2.py... Generating google/protobuf/internal/factory_test1_pb2.py... Generating google/protobuf/internal/factory_test2_pb2.py... Generating google/protobuf/pyext/python_pb2.py... bla...

生成google/protobuf/descriptor_pb2.py...生成google/protobuf/compiler/plugin_pb2.py...生成google/protobuf/unittest_pb2.py...生成google/protobuf/unittest_custom_options_pb2.py...生成google/protobuf /unittest_import_pb2.py... 生成 google/protobuf/unittest_import_public_pb2.py... 生成 google/protobuf/unittest_mset_pb2.py... 生成 google/protobuf/unittest_no_generic_services_pb2.py... 生成 google/protobuf/internal/.1_script_pb2.py ...生成google/protobuf/internal/descriptor_pool_test2_pb2.py...生成google/protobuf/internal/test_bad_identifiers_pb2.py...生成google/protobuf/internal/missing_enum_values_pb2.py...生成google/protobuf/internal/more_pb2extension .py ... 生成 google/protobuf/internal/more_extensions_dynamic_pb2。py...生成google/protobuf/internal/more_messages_pb2.py...生成google/protobuf/internal/factory_test1_pb2.py...生成google/protobuf/internal/factory_test2_pb2.py...生成google/protobuf/pyext/ python_pb2.py... bla...

Precisely the 'descriptor_pb2.py'

正是“descriptor_pb2.py”