typeerror: descriptors cannot not be created directly [SOLVED]

If you have encountered a Python error message “typeerror: descriptors cannot not be created directly” you are not alone. This error message can be frustrating for python developers, especially those who are beginners to Python programming.

In this article, we will explain what this error means, why it occurs, and how to solve it.

Here is an example of an error you may encounter:

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).

Why the error descriptors cannot not be created directly occur?

The error descriptors cannot not be created directly occur because the protobuf library initiate changes in version 4.21.0.

This error may occur If you are installing or running any Python program which uses the protobuf package.

What is protobuf Python?

Protobuf (short for Protocol Buffers) is a language-agnostic data serialization format developed by Google. It allows you to efficiently encode and decode structured data, making it easy to transmit data between different systems and programming languages.

Example of libraries packages has a protobuf:

  • tensorflow
  • ray
  • google-api
  • azureml

When you are using one of these above libraries, then you should solve the error in a two possibilities:

  • You need to downgrade the protobuf package to 3.20.x or lower
  • If you’re ML and AI libraries has already resolved the error, you need to upgrade them to the latest version.
  • You need to export the PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python variable.

In the next section we will see on how to solve the error descriptors can not be created directly.

How to solve the TypeError: Descriptors cannot not be created directly?

The simple way to solve this error is to downgrading the protobuf library installed on your Python system to version 3.20.x.

Solution 1: Downgrade protobuf version to 3.20

You can use one of the following commands to install protobuf version 3.20.x:

pip install protobuf==3.20

If you run the command it will download and install the packages.

C:\Users\Dell>pip install protobuf==3.20
Collecting protobuf==3.20
Using cached protobuf-3.20.0-cp39-cp39-win_amd64.whl (904 kB)
Installing collected packages: protobuf
Attempting uninstall: protobuf
Found existing installation: protobuf 3.19.6
Uninstalling protobuf-3.19.6:
Successfully uninstalled protobuf-3.19.6

You can use this command for Python3

pip3 install protobuf==3.20

If you already installed the protobuf, you can check the protobuf package version through running the pip show command:

pip show protobuf

If you run the command it will show an information such as name, location and version, etc.,

C:\Users\Dell>pip show 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: c:\users\dell\anaconda3\lib\site-packages
Requires:
Required-by: tensorboard, tensorflow-intel

If it is have a requirements.txt file in your python project folder, you can define a dependency to protobuf version 3.20.x with the following.

*** requirements.txt ***
protobuf==3.20

Through the above solution, the protobuf version will be compatible with your machine learning libraries.

Also, you might interested to read the other python error resolved:

Solution 2: Upgrade the version of the packages that causes the issue

If the error still persists, you can try to upgrade the package which is causing the issue.

Make sure to replace the tensorflow with the name of the package that will cause the issue in your program.

You can use this following command:

pip install tensorflow --upgrade
pip3 install tensorflow --upgrade

python -m pip install tensorflow --upgrade
python3 -m pip install tensorflow --upgrade
py -m pip install tensorflow --upgrade

The other libraries should have a common solved released, yet you should check their websites for more information.

Solution 3: Set the PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION environment variable

Another way to solve the error is to set the PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python environment variable to python as display in the error message.

You can use the following command:

For Linux / macOS:

export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python

For Windows Command Prompt:

setx PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION python

For PowerShell:

$Env:PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION="python"

Note: For setting the PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python environment variable will slow down your python script because the pure-Python parsing will be able to used.

Solution 4: Upgrade all packages in your environment

In upgrading all packages on your environment should be able to help because when the error is caused in an older version of a package that uses protobuf, a solution should have been implemented in more recent versions.

The most easiest way to upgrade all outdated packages is that we will use a Python script.

Through this code you can update all version of packages on your library.

import pkg_resources
from subprocess import call

packages = [dist.project_name for dist in pkg_resources.working_set]
call("pip install --upgrade " + ' '.join(packages), shell=True)

The following commands you can use to upgrade all outdated packages.

For macOS or Linux:

pip install -U `pip list --outdated | awk 'NR>2 {print $1}'`

For windows:

for /F "delims= " %i in ('pip list --outdated') do pip install -U %i

FAQs

What is a descriptor in Python?

A descriptor is an object attribute that defines how the value of another attribute can be accessed or modified in Python.

Conclusion

In conclusion, if you have encountered this typeerror: descriptors cannot not be created directly the above solutions will be able to help you to solve it.

Leave a Comment