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 workaroun
时间: 2023-07-20 17:15:15 浏览: 53
这个错误通常是由于protobuf版本不兼容造成的。出现这个错误的原因是因为你使用的protobuf版本太旧,无法创建描述符。
推荐使用最新版本的protobuf。如果无法立即更新protobuf版本,也可以尝试以下解决方案:
1. 重新生成_pb2.py文件
如果你的protobuf版本较老,可以尝试重新生成_pb2.py文件。可以通过以下命令来生成_pb2.py文件:
```
protoc --python_out=. your_proto_file.proto
```
其中,your_proto_file.proto是你的.proto文件的名称。
2. 使用旧版API
如果你无法更新protobuf版本或重新生成_pb2.py文件,可以尝试使用旧版API。在旧版API中,可以使用Descriptor()方法来创建描述符。
例如,下面是一个使用旧版API的Python代码示例:
```
import person_pb2
person_descriptor = person_pb2.Person.DESCRIPTOR
person_name_descriptor = person_descriptor.fields_by_name["name"]
person_age_descriptor = person_descriptor.fields_by_name["age"]
print(person_descriptor)
print(person_name_descriptor)
print(person_age_descriptor)
```
希望这些解决方案能够帮助你解决这个问题。