类型错误:无法直接创建描述符

has*_*u33 203 python ray tensorflow

我尝试安装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).
Run Code Online (Sandbox Code Playgroud)

我尝试解决问题并降级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
Run Code Online (Sandbox Code Playgroud)

但问题在 Ray、TensorFlowKeras中仍然存在。我的应用程序不再工作了。我该如何修复它?

Kus*_*era 244

有时,protobuf包可能会在您不参与的情况下安装。为此,您可以应用两种解决方案。尝试以下解决方案之一,它应该有效。

解决方案一:

您可以降级protobuf插件,

pip install protobuf==3.20.*
Run Code Online (Sandbox Code Playgroud)

或者您可以将其requirements.txt作为最后一个包添加到文件中。因为这会覆盖之前安装的protobuf包。

...
protobuf==3.20.*
Run Code Online (Sandbox Code Playgroud)

解决方案2:

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

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python
Run Code Online (Sandbox Code Playgroud)

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

但这将使用纯Python解析并且会慢很多


参考:

  • 遵循解决方案 01 和解决方案 02 后,两者都会导致我的程序运行速度变慢约 5 倍。我也尝试重建环境,但运行速度仍然很慢。我猜测,即使使用解决方案 01,某些东西也会强制“PROTOCOL_BUFFERS_PYTHON_IMPLMENTATION=python”,但我无法检查它。其他人也会发生这种情况吗?怎么才能回到原来的状态呢? (4认同)

小智 31

解决方案1:降级Protobuf

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

pip install --upgrade "protobuf<=3.20.1"
Run Code Online (Sandbox Code Playgroud)

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

pip install 'protobuf<=3.20.1' --force-reinstall
Run Code Online (Sandbox Code Playgroud)

解决方案 2:将 TensorFlow 更新到最新版本

TensorFlow 2.9.1 于 2022 年 5 月 23 日发布。它可以像这样更新:

pip install tensorflow==2.9.1
Run Code Online (Sandbox Code Playgroud)

始终使用最新版本的 TensorFlow。


小智 13

就我而言,我的文件中没有protobuf明确的说明requirements.txt,但我确实有一个相关的依赖项,这显然是有问题的:

googleapis-common-protos==1.6.0 # Depends on 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.
Run Code Online (Sandbox Code Playgroud)

屏幕片段


Blu*_*ese 5

命令

pip install protobuf==3.20.3 --upgrade
Run Code Online (Sandbox Code Playgroud)

为我工作。答案中提到的所有其他版本都没有解决问题。


小智 1

我发现了同样的问题,但我按照此网页上的说明进行操作:

Python 打包用户指南在 GitHub 上

脚步

要求/先决条件:

  1. 狮身人面像4.3.1

  2. sphinx-autobuild 0.7.1

  3. sphinx-inline-tabs 2021.4.11b9

  4. python-docs-主题 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
Run Code Online (Sandbox Code Playgroud)