Google Vision Python客户端快速启动报TypeError: bases must be types
问题描述
本地运行Google Vision Python库的quickstart示例,执行python3 quickstart.py命令时抛出如下报错:
Traceback (most recent call last): File "/Users/penguin/.../quickstart.py", line 53, in <module> print(run_quickstart()) File "/Users/penguin/.../quickstart.py", line 25, in run_quickstart from google.cloud import vision File "/Users/pengui/anaconda3/envs/vision/lib/python3.9/site-packages/google/cloud/vision/__init__.py", line 18, in <module> from google.cloud.vision_v1.services.image_annotator.async_client import ( File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/google/cloud/vision_v1/__init__.py", line 17, in <module> from google.cloud.vision_helpers.decorators import add_single_feature_methods File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/google/cloud/vision_helpers/__init__.py", line 16, in <module> import proto File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/proto/__init__.py", line 15, in <module> from .enums import Enum File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/proto/enums.py", line 17, in <module> from google.protobuf import descriptor_pb2 File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/google/protobuf/descriptor_pb2.py", line 5, in <module> from google.protobuf.internal import builder as _builder File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/google/protobuf/internal/builder.py", line 42, in <module> from google.protobuf import reflection as _reflection File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/google/protobuf/reflection.py", line 51, in <module> from google.protobuf import message_factory File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/google/protobuf/message_factory.py", line 42, in <module> from google.protobuf.internal import api_implementation File "/Users/penguin/anaconda3/envs/vision/lib/python3.9/site-packages/google/protobuf/internal/api_implementation.py", line 104, in <module> from google.protobuf.pyext import _message TypeError: bases must be types
已确认的前置信息
- Google Cloud Shell环境中运行同一份quickstart示例无异常
- 接口凭证配置正确,环境变量
GOOGLE_APPLICATION_CREDENTIALS=key.json设置无误 - 本地运行环境Python版本为3.9.12
- 已通过pip3安装requirements.txt指定的全部依赖,对应google-cloud-vision版本为2.7.2
python3与pip3均指向/anaconda3/envs/vision虚拟环境路径,不存在多环境混用问题- 本地操作系统为macOS Monterey 12.4
根因定位
报错触发点在protobuf C扩展加载环节,属于Anaconda环境下的典型依赖冲突:当前环境中安装的protobuf版本过高,和google-cloud-vision 2.7.2依赖的proto-plus库存在兼容性问题,部分通过conda源安装的protobuf版本还存在macOS平台C扩展编译适配问题,直接触发TypeError: bases must be types错误。
修复步骤
- 激活目标虚拟环境:
conda activate vision - 彻底卸载环境中已安装的冲突包:
pip uninstall -y protobuf proto-plus conda remove -y protobuf
- 安装经过兼容性验证的依赖版本:
pip install "protobuf>=3.19.0,<3.20.0" "proto-plus>=1.22.0"
- 校验安装结果:执行
pip list | grep proto,确认protobuf版本处于3.19.x区间、proto-plus版本不低于1.22.0即可。 - 重新执行
python3 quickstart.py即可正常运行。
注意:修复过程中不要使用conda安装protobuf相关依赖,conda源的protobuf包在macOS平台下容易出现C扩展适配问题,直接通过pip安装官方预编译的wheel包可规避该类问题。
内容的提问来源于stack exchange,提问作者William Torkington
相关产品推荐
相关产品推荐

