File "/home/yq/anaconda3/envs/Patchfusion/lib/python3.7/site-packages/google/protobuf/descriptor.py", line 561, 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
时间: 2025-06-13 08:19:54 浏览: 18
<think>好的,我现在要解决用户遇到的“Descriptors cannot not be created directly”错误。根据用户提供的引用信息,这个问题主要与protobuf的版本不兼容有关。首先,我需要理解这个错误的原因。
用户提到的引用[1]、[2]、[3]、[4]都指出,错误是由于protobuf的版本过高导致的。例如,引用[2]和[3]建议安装3.19.0版本,而引用[1]提到3.20.*版本。用户可能在使用某些库(如TensorFlow)时,这些库依赖特定版本的protobuf,如果版本过高,就会引发此错误。
接下来,我需要确定解决步骤。首先,用户需要卸载当前安装的protobuf,然后安装指定版本。根据引用中的建议,3.19.0或3.20.*都是可行的,但可能需要根据具体情况选择。例如,引用[4]提到TensorFlow 2.6.0需要降低protobuf版本,用户可能需要检查他们使用的库的文档以确定兼容版本。
此外,用户可能需要使用虚拟环境来管理依赖,避免全局安装的冲突。引用中提到的用pip安装指定版本的方法是正确的,但需要确保安装后其他依赖不会自动升级protobuf,否则问题可能再次出现。
最后,验证安装是否成功,可以通过检查版本号或运行代码来确认错误是否解决。如果问题依旧,可能需要进一步检查其他依赖项的兼容性。</think>### 如何解决"Descriptors cannot not be created directly"错误
#### 错误原因分析
该错误由protobuf(Protocol Buffers)的版本冲突引起,常见于TensorFlow等依赖protobuf的库中。新版本protobuf(≥4.0)的语法和接口可能与旧版本框架不兼容,导致描述符创建失败[^2][^4]。
#### 解决方案步骤
1. **卸载当前protobuf版本**
```bash
pip uninstall protobuf
```
2. **安装指定兼容版本**(根据框架需求选择)
- **TensorFlow 2.6.0推荐方案**:
```bash
pip install protobuf==3.19.0
```
[^3]
- **通用兼容方案**:
```bash
pip install protobuf==3.20.*
```
[^1]
3. **验证安装结果**
```bash
pip show protobuf # 应显示Version: 3.19.0或3.20.x
```
4. **创建虚拟环境**(可选但推荐)
```bash
python -m venv myenv # 创建虚拟环境
source myenv/bin/activate # 激活环境(Linux/Mac)
```
#### 注意事项
- 如果使用TensorFlow,建议同时检查其版本兼容性:
```bash
pip install tensorflow==2.6.0 # 需与protobuf版本匹配
```
- 使用`requirements.txt`管理依赖可避免重复出现该问题[^2]
阅读全文
相关推荐



















