python出现If this call came from a _pb2.py file, your generated...

1年前 ⋅ 1531 阅读

执行python代码实现功能的时候,出现如下的错误:

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

意思是说你的代码过期,必须找一个版本比3.19.0这个版本还要新的版本。总之,就是版本过旧了,安装新版本即可。那么如何解决?办法很简单,如下:
解决办法
在控制台窗口中更新安装新的版本的模块,命令如下:
1.如果你不确定自己想要安装哪个版本的模块,直接运行下面代码

pip3 install --upgrade protobuf #直接安装最新版本

2.如果直接安装最新的模块和别的依赖模块起冲突了,这个时候需要安装指定的模块,运行下面代码即可

pip3 install --upgrade protobuf==3.20.1 # 3.20.1表示指定版本号,这里换成你自己的版本号就可以了