【protobuf 报错】TypeError: Descriptors cannot be created directly.
·
报错信息:
TypeError: Descriptors cannot 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).
More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
根据您遇到的错误信息,似乎是在使用Protocol Buffers(protobuf)时遇到的一个问题。这个错误提示您的protobuf Python库可能需要更新,或者您的.proto文件需要重新编译。下面是几个可能的解决方案:
- 更新protobuf库:
根据错误信息,您需要确保您的protobuf库版本是3.19.0或更高。您可以使用pip来更新protobuf库:pip install --upgrade protobuf - 重新编译.proto文件:
如果更新protobuf库后问题仍然存在,您可能需要重新编译您的.proto文件。确保您使用的protoc编译器版本是3.19.0或更高。编译命令通常如下:protoc --python_out=. your_file.proto - 降级protobuf库:
如果您不能立即重新编译.proto文件,您可以考虑临时降级protobuf库到3.20.x或更低版本。使用pip可以降级库:pip install protobuf==3.20.0 - 使用Python实现:
另一个可能的临时解决方案是设置环境变量PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION为python。这将使用纯Python解析,可能会慢得多,但可以作为一个临时解决方案。export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python - 查看更多详细信息:
错误信息中还提供了一个链接,您可以通过这个链接查看更多的更新信息和可能的解决方案:protobuf Python updates。
请尝试上述解决方案,并找到适合您情况的方法。如果问题仍然存在,可能需要更详细地检查您的代码和库的版本兼容性。
更多推荐
所有评论(0)