类型错误: 描述符不能直接创建

194

我尝试安装Ray,但出现错误:

TypeError: Descriptors cannot not be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
 1. Downgrade the protobuf package to 3.20.x or lower.
 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

我尝试解决问题并降级了protobuf

Name: protobuf
Version: 3.20.0
Summary: Protocol Buffers
Home-page: https://developers.google.com/protocol-buffers/
Author:
Author-email:
License: BSD-3-Clause
Location: d:\opencv\lib\site-packages
Requires:
Required-by: ray, tensorboard, tensorflow

但是问题仍然存在于 Ray、TensorFlowKeras 中。我的应用程序不再工作。我该如何解决?


也许尝试较低版本的protobuf - 例如3.193.18 - furas
3
我将 protobuf4.21.1 降级到 3.20.1。https://github.com/protocolbuffers/protobuf/issues/10051 - YugoAmaryl
1
对于未来的读者,我降级到了3.20.0版本,这解决了我的问题。3.21.1对我来说不起作用(我遇到了相同的错误)。 - null
16个回答

237

有时会自动安装protobuf包。对此,您有两个解决方案可供选择。尝试以下其中一个解决方案即可。

解决方案1:

您可以将protobuf插件降级,

pip install protobuf==3.20.*

或者您可以将它添加到requirements.txt文件中作为最后一个软件包。由于这样会覆盖先前安装的protobuf软件包。

...
protobuf==3.20.*

解决方案2:

您可以设置以下环境变量。

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python

根据错误描述,这可能会影响您的程序性能。

但这将使用纯Python解析,速度会慢得多。


参考资料:


4
在尝试了解决方案01和解决方案02之后,我的程序运行速度变慢了约5倍。我还尝试重新构建环境,但速度仍然很慢。我猜测即使使用解决方案01,某些东西仍在强制使用PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python,但我无法检查。是否有其他人也遇到同样的问题?我该如何回到最初的状态? - Guillem
1
你说得对 @Guillem ,解决方案02可能会影响程序性能,他们在错误描述中也提到了。但是解决方案01应该可以工作。 - Kushan Gunasekera
问题在于解决方案01也影响了性能。我甚至尝试重新安装conda,但代码运行速度仍然不如以前快。 - Guillem
@Guillem,不可能的,我也做了同样的事情,但我没有在我的程序中发现任何性能问题。对此感到抱歉。 - Kushan Gunasekera
1
当旧版本不再受支持时会发生什么? - jtlz2
1
我在使用AWS SageMaker时遇到了这个问题,解决方案1对我有效。 - Yuri Santos

24

解决方案1:降级Protobuf

这个库最近发布了一个新版本,导致出现错误。在终端中使用以下命令进行降级,应该可以解决问题:

pip install --upgrade "protobuf<=3.20.1"

或者强制重新安装旧版本:

pip install 'protobuf<=3.20.1' --force-reinstall

解决方案二:更新 TensorFlow 至最新版本

TensorFlow 2.9.1 版本于2022年5月23日发布,可通过以下方式进行更新:

pip install tensorflow==2.9.1

始终使用最新版本的TensorFlow。


实际上,对于tensorflow 2.9.1,您应该使用protobuf <3.20: https://github.com/chxin66/tensorflow/commit/c2a8a98b764ac8f0352cac0a438007c899af8f96#diff-f526feeafa1000c4773410bdc5417c4022cb2c7b686ae658b629beb541ae9112L90 - scottlittle
解决方案1对我有效。 - Malgo
强制重新安装对我起了作用。 - chaytan

13

在我的情况下,我并没有在我的requirements.txt文件中明确地添加protobuf,但是我有一个相关的依赖项似乎存在问题:

googleapis-common-protos==1.6.0 # 依赖于 protobuf

移除了这个依赖后,随后的pip install -e .(假设setup.py存在)就可以使用google-api-core==1.13.0所需的任何依赖项进行安装。这导致googleapis-common-protos == 1.56.2被安装并解决了错误。


13

我也遇到了同样的错误

我通过安装protobuf解决了这个问题

pip install protobuf==3.20.*

Screen Snippet


5

该命令

pip install protobuf==3.20.3 --upgrade

对我有用。其他答案提到的所有版本都没有解决问题。


4

不能直接创建描述符:

  1. 我们需要将protobuf软件包从4.21.2降级到3.20.1。
  2. 前往设置→ 项目Python解释器 → 安装protobuf 3.20.1(指定版本)

4

或者,我成功升级到wandb==0.12.17,现在一切似乎都正常了。

旧版本(不可用):

  - pip:
      - wandb==0.10.21

新(运行中):
  - pip:
      - wandb==0.12.17

3
以下方法适用于我在tensorflow 2.8上的操作。
pip install protobuf==3.20.*

1

我在升级谷歌云日志后遇到了同样的问题。我通过升级google-cloud-audit-log解决了这个问题。

pip install google-cloud-audit-log==0.2.4

可用版本:

pip freeze | grep google

google-api-core==2.10.2
google-api-python-client==2.57.0
google-auth==2.10.0
google-auth-httplib2==0.1.0
google-auth-oauthlib==0.5.2
google-cloud-appengine-logging==1.1.6
google-cloud-audit-log==0.2.4
google-cloud-core==2.3.2
google-cloud-logging==3.2.5
google-cloud-storage==2.5.0
google-crc32c==1.1.2
google-resumable-media==2.4.0
googleapis-common-protos==1.56.4
grpc-google-iam-v1==0.12.4

希望这能帮到你。干杯!


1

我遇到了同样的问题,但是我按照这个网页上的说明进行操作:

Python Packaging User Guide (on GitHub)

步骤

要求 / 先决条件:

  1. sphinx 4.3.1

  2. sphinx-autobuild 0.7.1

  3. sphinx-inline-tabs 2021.4.11b9

  4. python-docs-theme 2021.5

  5. sphinx-copybutton 0.4.0

  6. python -V 的输出结果为:

    Python 3.8.10

  7. protoc.exe --version 的输出结果为:

    libprotoc 3.21.0-rc2

安装:

  1. pip install git+https://github.com/pypa/pypa-docs-theme.git#egg=pypa-docs-theme

  2. 编译库

  3. 将编译好的库从 F:\temp\Python\protoc\packaging.python.org\protobuf\Debug 复制到 F:\temp\Python\protoc\packaging.python.org\protobuf\src

输出:

installing library code to build\bdist.win-amd64\egg
running install_lib
running build_py
creating build\lib.win-amd64-3.8
creating build\lib.win-amd64-3.8\google
copying google\__init__.py -> build\lib.win-amd64-3.8\google
creating build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\any_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\api_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\descriptor.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\descriptor_database.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\descriptor_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\descriptor_pool.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\duration_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\empty_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\field_mask_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\json_format.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\message.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\message_factory.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\proto_builder.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\reflection.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\service.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\service_reflection.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\source_context_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\struct_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\symbol_database.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\text_encoding.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\text_format.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\timestamp_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\type_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\unknown_fields.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\wrappers_pb2.py -> build\lib.win-amd64-3.8\google\protobuf
copying google\protobuf\__init__.py -> build\lib.win-amd64-3.8\google\protobuf

...
testTimestampSerializeAndParse (google.protobuf.internal.well_known_types_test.TimeUtilTest) ... ok
testTimezoneAwareDatetimeConversionLA (google.protobuf.internal.well_known_types_test.TimeUtilTest)
testTimezoneAwareDatetimeConversionLA([1969, 12, 31, 18], datetime.timezone(datetime.timedelta(days=-1, seconds=57600), 'US/Pacific')) ... ok
testTimezoneAwareDatetimeConversionLondon (google.protobuf.internal.well_known_types_test.TimeUtilTest)
testTimezoneAwareDatetimeConversionLondon([1970, 1, 1, 2], datetime.timezone.utc) ... ok
testTimezoneAwareDatetimeConversionTokyo (google.protobuf.internal.well_known_types_test.TimeUtilTest)
testTimezoneAwareDatetimeConversionTokyo([1970, 1, 1, 11], datetime.timezone(datetime.timedelta(seconds=32400), 'Japan')) ... ok
testTimezoneNaiveDatetimeConversion (google.protobuf.internal.well_known_types_test.TimeUtilTest) ... ok
testByteSizeFunctions (google.protobuf.internal.wire_format_test.WireFormatTest) ... ok
testPackTag (google.protobuf.internal.wire_format_test.WireFormatTest) ... ok
testUnpackTag (google.protobuf.internal.wire_format_test.WireFormatTest) ... ok
testZigZagDecode (google.protobuf.internal.wire_format_test.WireFormatTest) ... ok
testZigZagEncode (google.protobuf.internal.wire_format_test.WireFormatTest) ... ok

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接