You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已安装google-generativeai却遇模块错误,求排查Python版本适配问题

问题排查:Google Generative AI SDK 适配问题

我运行以下Python脚本:

# Install from PyPI.

# pip install -U google-generativeai

# Import the SDK and configure your API key
import os
import google.generativeai as genai

genai.configure(api_key="MY_GOOGLE_API_KEY")

# Create a model and run a prompt.

model = genai.GenerativeModel('gemini-1.5-flash')
response = model.generate_content("The opposite of hot is")
print(response.text)

使用Python 3.8时,执行model = genai.GenerativeModel('gemini-1.5-flash')这一行出现错误:

AttributeError: module 'google.generativeai' has no attribute 'GenerativeModel'

切换至Python 3.12.4后,执行import google.generativeai as genai这一行出现新错误:

ModuleNotFoundError: No module named 'google'

执行pip list | grep google的结果如下:

google-ai-generativelanguage  0.1.0
google-api-core               2.19.1
google-api-python-client      2.136.0
google-auth                   2.30.0
google-auth-httplib2          0.2.0
google-auth-oauthlib          1.2.0
google-cloud-aiplatform       1.57.0
google-cloud-bigquery         3.25.0
google-cloud-core             2.4.1
google-cloud-resource-manager 1.12.3
google-cloud-storage          2.17.0
google-crc32c                 1.5.0
google-generativeai           0.1.0rc1
google-resumable-media        2.7.1
googleapis-common-protos      1.63.2
grpc-google-iam-v1            0.13.1

我怀疑问题与Python版本适配相关,但无法定位具体原因,恳请协助排查解决。

内容的提问来源于stack exchange,提问作者tryflyhi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 17:36:14