Windows环境下导入Python BigQuery库失败问题求助
解决导入google-cloud-bigquery时的
message_types_by_name属性错误 问题详情
执行from google.cloud import bigquery时触发以下错误:
Script failed to evaluate(): 'NoneType' object has no attribute 'message_types_by_name' Traceback (most recent call last): File "Python Script", line 4, in <module> from google.cloud import bigquery File "__init__.py", line 35, in <module> from google.cloud.bigquery.client import Client File "client.py", line 60, in <module> import google.api_core.exceptions as core_exceptions File "exceptions.py", line 29, in <module> from google.rpc import error_details_pb2 File "error_details_pb2.py", line 39, in <module> _ERRORINFO = DESCRIPTOR.message_types_by_name["ErrorInfo"] AttributeError: 'NoneType' object has no attribute 'message_types_by_name'
环境信息
- Python版本:3.8.1(64位)
- 安装命令:
"...\python\python38\python" -m pip install --no-cache-dir --upgrade --target="...\some\directory" google-cloud-storage google-cloud-bigquery[pandas]
- 已尝试方案:安装
google-cloud-bigquery[pandas]==3.11.4、添加protobuf依赖,均无效 - 可正常执行:
from google.oauth2 import service_account
当前依赖列表(pip freeze --path ...\some\directory输出)
cachetools==5.3.1 certifi==2023.7.22 charset-normalizer==3.3.0 db-dtypes==1.1.1 google-api-core==2.12.0 google-auth==2.23.3 google-cloud-bigquery==3.11.4 google-cloud-core==2.3.3 google-cloud-storage==2.11.0 google-crc32c==1.5.0 google-resumable-media==2.6.0 googleapis-common-protos==1.60.0 grpcio==1.59.0 idna==3.4 numpy==1.24.4 packaging==23.2 pandas==2.0.3 proto-plus==1.22.3 protobuf==4.24.4 pyarrow==13.0.0 pyasn1==0.5.0 pyasn1-modules==0.3.0 python-dateutil==2.8.2 pytz==2023.3.post1 requests==2.31.0 rsa==4.9 six==1.16.0 tzdata==2023.3 urllib3==2.0.6
解决方案
1. 降级Protobuf到兼容版本
当前使用的protobuf==4.24.4与Python3.8、google-api-core==2.12.0存在兼容性问题,建议降级到3.20.x版本:
"...\python\python38\python" -m pip install --no-cache-dir --target="...\some\directory" protobuf==3.20.3
2. 确保目标目录依赖优先加载
在Python脚本开头添加以下代码,强制优先加载指定目录的依赖,避免全局环境干扰:
import sys sys.path.insert(0, "...\some\directory")
3. 清理后重新安装完整依赖组合
若降级后仍有问题,删除目标目录下所有文件,重新安装指定版本的依赖包,确保版本匹配:
"...\python\python38\python" -m pip install --no-cache-dir --target="...\some\directory" google-cloud-storage google-cloud-bigquery[pandas]==3.11.4 protobuf==3.20.3 google-api-core==2.12.0 googleapis-common-protos==1.60.0
4. 验证Protobuf版本
执行以下命令确认目标目录的Protobuf版本为3.20.x:
"...\python\python38\python" -c "import sys; sys.path.insert(0, '...\some\directory'); import protobuf; print(protobuf.__version__)"
内容的提问来源于stack exchange,提问作者Christoph H.
相关产品推荐
相关产品推荐

