在Python Anywhere中用Flask+gspread遇‘google.auth.api_key缺失’错误求助
File "/home/vishal6596/mysite/flask_app.py", line 7, in
import gspread
File "/home/vishal6596/.local/lib/python3.10/site-packages/gspread/init.py", line 7, in
from .auth import (
File "/home/vishal6596/.local/lib/python3.10/site-packages/gspread/auth.py", line 14, in
from google.auth.api_key import Credentials as APIKeyCredentials
ModuleNotFoundError: No module named 'google.auth.api_key'
升级google-auth库:
google.auth.api_key模块是较新版本的google-auth才引入的,Python Anywhere上的版本可能过低。执行对应Python3.10的升级命令:pip3.10 install --upgrade google-auth同步依赖版本:本地PyCharm能正常运行,说明本地依赖版本兼容。在本地执行
pip freeze > requirements.txt生成依赖清单,然后在Python Anywhere上运行:pip3.10 install -r requirements.txt确保所有依赖包版本和本地完全一致。
检查安装环境与路径:确认Python Anywhere的Flask运行环境使用的是Python3.10。如果使用了虚拟环境,先激活再安装依赖:
source /home/vishal6596/your_virtual_env/bin/activate pip install --upgrade gspread google-auth未使用虚拟环境的话,添加
--user参数确保安装到当前用户的Python3.10路径下:pip3.10 install --user --upgrade gspread google-auth重装关联依赖:直接卸载并重新安装所有相关包,修复可能的损坏或缺失:
pip3.10 uninstall -y gspread google-auth google-auth-oauthlib pip3.10 install gspread google-auth google-auth-oauthlib
内容的提问来源于stack exchange,提问作者VISHAL SHARMA

