解决Google API与Gspread的Python包依赖冲突问题
解决gspread与Google Analytics API的依赖冲突问题
核心原因
你遇到的冲突根源在于:
- gspread 5.7.0强制要求
google-auth==1.12.0 - Google Analytics Reporting API v4依赖的
google-api-python-client 2.50.0和google-api-core 2.8.1需要更高版本的google-auth(通常要求>=2.1.0)
最佳解决方法(推荐)
升级gspread到最新稳定版,新版本已移除对google-auth的严格版本限制,可兼容更高版本的google-auth,同时满足GA API的需求:
- 卸载现有gspread:
pip uninstall -y gspread
- 安装最新版gspread:
pip install gspread --upgrade
- 升级GA API相关依赖包到兼容版本:
pip install --upgrade google-auth google-api-python-client google-api-core
执行完以上步骤后,两个工具的依赖即可兼容,既能正常调用GA API,也能使用gspread操作Google Sheets。
备选方案(若需保留gspread 5.7.0)
如果必须使用gspread 5.7.0,可降级GA API相关依赖包到兼容google-auth 1.12.0的版本:
- 卸载现有冲突的GA API依赖:
pip uninstall -y google-api-python-client google-api-core
- 安装兼容旧版
google-auth的指定版本:
pip install google-api-python-client==2.30.0 google-api-core==2.2.2
- 确保gspread版本为5.7.0:
pip install gspread==5.7.0
新手友好建议
无论采用哪种方案,都建议使用Python虚拟环境隔离项目依赖,避免全局包冲突:
- 在Jupyter Notebook中,可通过conda或venv创建虚拟环境,激活环境后再安装所需包。
内容的提问来源于stack exchange,提问作者Carson Whitley
相关产品推荐
相关产品推荐

