在GCP的Jupyter Notebook中导入bigquery遇bases must be types错误
解决导入
google.cloud.bigquery时的TypeError: bases must be types错误 问题复现
执行以下代码时触发错误:
from google.cloud import bigquery
报错堆栈核心指向google.protobuf.pyext._message导入失败:
TypeError: bases must be types
可能原因
这个错误通常由以下情况导致:
- Protobuf包的C扩展模块编译损坏或版本不兼容(比如高版本Protobuf不再支持Python3.7)
- 之前的包安装/卸载操作破坏了依赖链,导致依赖包版本冲突
- Conda或Pip环境中存在重复的Protobuf相关包
解决方案
1. 重装兼容版本的Protobuf
Python3.7对高版本Protobuf兼容性有限,建议安装3.20.x系列版本:
# 先卸载现有Protobuf pip uninstall -y protobuf # 安装兼容版本 pip install protobuf==3.20.3
如果使用Conda环境:
conda remove protobuf conda install protobuf=3.20.3
2. 确保google-cloud-bigquery版本兼容
安装与Python3.7和Protobuf3.20匹配的BigQuery客户端版本:
pip install google-cloud-bigquery==3.10.0
3. 强制重装并清理缓存
如果扩展模块损坏,强制重装Protobuf并清理编译缓存:
pip install --force-reinstall --no-cache-dir protobuf==3.20.3
4. 创建干净的Conda环境(推荐)
如果环境冲突严重,直接创建新环境隔离问题:
# 创建新环境 conda create -n bigquery_env python=3.7 # 激活环境 conda activate bigquery_env # 安装依赖 pip install google-cloud-bigquery
内容的提问来源于stack exchange,提问作者runner16
相关产品推荐
相关产品推荐

