TensorFlow2.8.0报错TypeError: Descriptors cannot not be created directly.

@创建于:2022.09.28
@修改于:2022.09.28

1、错误信息

Traceback (most recent call last):
  File "D:\PycharmProjects\ner\test.py", line 15, in <module>
    import tensorflow as tf
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\tensorflow\__init__.py", line 37, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\__init__.py", line 37, in <module>
    from tensorflow.python.eager import context
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\tensorflow\python\eager\context.py", line 29, in <module>
    from tensorflow.core.framework import function_pb2
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\tensorflow\core\framework\function_pb2.py", line 16, in <module>
    from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\tensorflow\core\framework\attr_value_pb2.py", line 16, in <module>
    from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\tensorflow\core\framework\tensor_pb2.py", line 16, in <module>
    from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\tensorflow\core\framework\resource_handle_pb2.py", line 16, in <module>
    from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\tensorflow\core\framework\tensor_shape_pb2.py", line 36, in <module>
    _descriptor.FieldDescriptor(
  File "D:\ProgramData\Anaconda3\envs\tf\lib\site-packages\google\protobuf\descriptor.py", line 560, in __new__
    _message.Message._CheckCalledFromGeneratedFile()
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).

More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates

Process finished with exit code 1

pip安装tensorflow的时候,依赖包protobuf的版本过高,4.21.6

2、解决办法

# 卸载当前版本(我的是4.21.6)
pip uninstall protobuf
# 安装低版本
pip install protobuf==3.19.0

3、参考链接

新装的TensorFlow出现protobuf相关错误

你可能感兴趣的:(Tensorflow2.X,/,Keras,tensorflow,protobuf)