-
Notifications
You must be signed in to change notification settings - Fork 487
google.generativeai 函式庫在 Python 3.12.1 (Windows) 環境下,genai.Client 屬性缺失 (即使版本正確且在虛擬環境中) #734
Description
Description of the bug:
環境 (Environment):
作業系統 (OS): Windows [版本 Windows 11 家用版
版本 24H2
安裝於 2025/2/26
OS 組建 26100.4061
體驗 Windows 功能體驗套件 1000.26100.84.0]
Python 版本 (Python Version): 3.12.1 (64-bit, 從 python.org 官方下載安裝)
google-generativeai 函式庫版本 (Library Versions Tested): 0.8.5, 0.7.1, 0.5.0
問題描述 (Problem Description):
在上述環境中,無論安裝 google-generativeai 函式庫的哪個版本 (已測試 0.8.5, 0.7.1, 0.5.0),在 Python 互動模式或腳本中 import google.generativeai as genai 後,檢查 hasattr(genai, 'Client') 的結果始終為 False,且 dir(genai) 的輸出中不包含 Client。
然而,函式庫的其他部分如 genai.GenerativeModel 則可以正常存取和使用,genai.version 和 genai.file 也顯示為正確的已安裝版本和路徑。
此問題即使在全新的 Python 虛擬環境 (venv) 中,使用 pip install --no-cache-dir google-generativeai 進行了乾淨安裝後,依然穩定重現。
重現問題的步驟 (Steps to Reproduce):
(以下步驟在全新的 Python 虛擬環境中進行,以 google-generativeai==0.8.5 為例,其他測試版本有相同結果)
建立並啟動新的 Python 虛擬環境:
(假設 Python 3.12.1 安裝路徑為 C:\Users\YourName\AppData\Local\Programs\Python\Python312\python.exe)
Bash
在 Windows 命令提示字元中
"C:\Users\Wei Jane\AppData\Local\Programs\Python\Python312\python.exe" -m venv my_test_env
my_test_env\Scripts\activate
在虛擬環境中安裝 google-generativeai (例如最新版,或指定版本如 0.8.5 / 0.7.1 / 0.5.0):
Bash
pip install --no-cache-dir google-generativeai
或者安裝特定版本:
Bash
pip install --no-cache-dir google-generativeai==0.8.5
啟動 Python 互動模式並執行檢查:
Bash
python
然後在 Python 互動模式中輸入:
Python
import google.generativeai as genai
print(f"google-generativeai 版本: {genai.version}")
print(f"模組檔案路徑: {genai.file}")
print(f"是否有 Client 屬性: {hasattr(genai, 'Client')}")
print(f"是否有 GenerativeModel 屬性: {hasattr(genai, 'GenerativeModel')}")
print("\ndir(genai) 輸出:")
print(dir(genai))
exit()
預期行為 (Expected Behavior):
hasattr(genai, 'Client') 應該返回 True,並且 genai.Client 可以被正常使用。dir(genai) 列表中應包含 Client。
實際行為 (Actual Behavior):
hasattr(genai, 'Client') 返回 False。dir(genai) 的輸出列表中不包含 Client。
以下為一次在虛擬環境中安裝 0.8.5 版本後的互動模式輸出範例:
版本 (虛擬環境, 無快取): 0.8.5
檔案路徑 (虛擬環境, 無快取): D:\Wei-Jane C\Python\my_discord_bot\mybot_env\Lib\site-packages\google\generativeai_init_.py
是否有 Client (虛擬環境, 無快取): False
是否有 GenerativeModel (虛擬環境, 無快取): True
模組內可用的屬性 (虛擬環境):
['ChatSession', 'GenerationConfig', 'GenerativeModel', 'builtins', 'cached', 'doc', 'file', 'loader', 'name', 'package', 'path', 'spec', 'version', 'annotations', 'caching', 'configure', 'create_tuned_model', 'delete_file', 'delete_tuned_model', 'embed_content', 'embed_content_async', 'get_base_model', 'get_file', 'get_model', 'get_operation', 'get_tuned_model', 'list_files', 'list_models', 'list_operations', 'list_tuned_models', 'protos', 'responder', 'string_utils', 'types', 'update_tuned_model', 'upload_file', 'utils']
已嘗試的除錯步驟 (Troubleshooting Steps Taken):
確認使用正確的 Python 直譯器完整路徑執行 (C:\Users\Wei Jane\AppData\Local\Programs\Python\Python312\python.exe)。
在全域環境和全新的虛擬環境 (venv) 中進行測試。
嘗試了 google-generativeai 的多個版本 (0.8.5, 0.7.1, 0.5.0)。
使用了 pip install --upgrade google-generativeai。
使用了 pip install --no-cache-dir --force-reinstall google-generativeai。
手動從 site-packages 刪除 google_generativeai 和 google_ai_generativelanguage 相關資料夾後再重新安裝。
檢查了 genai.file 和 genai.version 確認模組載入路徑和版本正確。
確認了 genai.GenerativeModel 可以正常載入和使用。
嘗試直接導入子模組 from google.generativeai import client,雖然子模組 client.py 看似能被找到 (根據其 file 路徑),但該子模組物件內仍不包含 Client 類別。
Actual vs expected behavior:
No response
Any other information you'd like to share?
No response