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 – How To Fix?

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 are confused about “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”, take a look at our instructions below to get the answer.

Reason for “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”.

Recently, a new version 4.21.0 of Protocol Buffers was released. There are some changes in Python language support in Protocol Buffers. Specially, one of them requires protoc 3.19.0 or newer and update more information.

Suppose your protobuf’s version is higher than 3.20.x, you will be in trouble when you import some frameworks or libraries such as tensorflow, tensor-hub, mediapine, google-cloud-bigquery…

Code:

import tensorflow as tf

print(tf.constant([1, 2, 3, 4, 5, 6]))

Result:

Traceback (most recent call last)
File "code.py", line 1, in <module>
    import tensorflow as tf
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.

To know more about the current protobuf’s version, you can check by command:

pip show protobuf

Result:

Name: protobuf
Version: 4.21.0
Summary: None
Home-page: None
Author: None
Author-email: None
License: None
Location: /usr/local/lib/python3.7/dist-packages
Requires: 
Required-by: tensorflow, tensorflow-metadata, tensorflow-hub, tensorflow-datasets, tensorboard, googleapis-common-protos, google-cloud-bigquery, google-cloud-bigquery-storage, google-api-core

Solutions to the problem

Downgrade the protobuf’s version

As instructed above, you can downgrade the version of protobuf to 3.20.x or lower to suit the current software version by using pip directly in your terminal. We suggest this as the simplest and the most effective method to solve the problem.

Command:

pip install protobuf==3.20

Change environment variable to python

Another solution to the problem is that you can change the environment variable from cpp to python with the command:

Export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python 

As the publisher’s instruction, this solution is not recommended because the default value is cpp faster than python.

The result after reconfiguration:

<tf.Tensor: shape=(6,), dtype=int32, numpy=array([1, 2, 3, 4, 5, 6], dtype=int32)>

Summary

This is an error that occurs after the update of Protocol Buffers. Through the article you have known the cause of the eror “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” happens. We strongly recommend downgrading your protobuf’s version as the most effective way.

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *