Github Actions部署Google Palm API遇'google.protobuf'模块缺失错误
解决Github Actions中安装google-generativeai时的
google.protobuf缺失问题 问题详情
在Github Actions里安装google-generativeai库调用Palm API,结果触发ModuleNotFoundError,提示找不到google.protobuf模块。试过三种办法都没用:
- 在Actions的YAML文件里直接执行
pip install protobuf - 把
protobuf添加进requirements.txt - 在
setup.py里配置该依赖
完整报错堆栈:
Hint: make sure your test modules/packages have valid Python names. Traceback: /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/importlib/__init__.py:127: in import_module return _bootstrap._gcd_import(name[level:], package, level) tests/test_adapt_decorator.py:6: in <module> from .model import GPT4, Claude tests/model.py:8: in <module> import google.generativeai as palm /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/google_generativeai-0.1.0-py3.9.egg/google/generativeai/__init__.py:69: in <module> from google.generativeai import types /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/google_generativeai-0.1.0-py3.9.egg/google/generativeai/types/__init__.py:17: in <module> from google.generativeai.types.discuss_types import * /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/google_generativeai-0.1.0-py3.9.egg/google/generativeai/types/discuss_types.py:21: in <module> import google.ai.generativelanguage as glm /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/google_ai_generativelanguage-0.2.0-py3.9.egg/google/ai/generativelanguage/__init__.py:21: in <module> from google.ai.generativelanguage_v1beta2.services.discuss_service.async_client import ( /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/google_ai_generativelanguage-0.2.0-py3.9.egg/google/ai/generativelanguage_v1beta2/__init__.py:21: in <module> from .services.discuss_service import DiscussServiceAsyncClient, DiscussServiceClient /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/google_ai_generativelanguage-0.2.0-py3.9.egg/google/ai/generativelanguage_v1beta2/services/discuss_service/__init__.py:16: in <module> from .async_client import DiscussServiceAsyncClient /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/google_ai_generativelanguage-0.2.0-py3.9.egg/google/ai/generativelanguage_v1beta2/services/discuss_service/async_client.py:31: in <module> from google.api_core import exceptions as core_exceptions /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/google_api_core-2.12.0.dev0-py3.9.egg/google/api_core/exceptions.py:29: in <module> from google.rpc import error_details_pb2 /opt/hostedtoolcache/Python/3.9.17/x64/lib/python3.9/site-packages/googleapis_common_protos-1.59.1-py3.9.egg/google/rpc/error_details_pb2.py:20: in <module> from google.protobuf import descriptor as _descriptor E ModuleNotFoundError: No module named 'google.protobuf'
可行解决方案
1. 强制重装指定版本的protobuf
旧版本protobuf可能与google-generativeai不兼容,先卸载再安装高版本:
- name: 安装依赖 run: | pip uninstall -y protobuf pip install protobuf>=4.21.0 google-generativeai
2. 用虚拟环境隔离依赖
Github Actions的全局Python环境可能存在残留依赖,用虚拟环境安装项目专属依赖:
- name: 配置虚拟环境 run: | python -m venv venv source venv/bin/activate pip install --upgrade pip pip install -r requirements.txt
3. 检查setup.py的依赖声明
确保setup.py的install_requires明确指定protobuf版本,避免版本模糊:
setup( # 其他配置项... install_requires=[ "google-generativeai>=0.1.0", "protobuf>=4.21.0" ] )
4. 清理pip缓存后重装
pip缓存的旧包可能导致安装异常,先清理缓存再重新安装:
- name: 清理pip缓存 run: pip cache purge - name: 安装依赖 run: pip install -r requirements.txt
问题原因
从报错堆栈来看,并非未安装protobuf,大概率是以下问题导致:
- 版本冲突:旧版protobuf与google-generativeai依赖的版本不匹配
- 环境污染:全局Python环境中的旧依赖与项目依赖冲突
- 缓存问题:pip缓存的旧版本未更新,导致实际安装的包不符合要求
内容的提问来源于stack exchange,提问作者Soubriquet
相关产品推荐
相关产品推荐

