【问题标题】:AttributeError: 'module' object has no attribute 'authorize'AttributeError:“模块”对象没有属性“授权”
【发布时间】:2017-11-11 09:30:44
【问题描述】:

我想向谷歌电子表格报告一些东西,例如我的代码:

import json
import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ['https://spreadsheets.google.com/feeds']

credentials = ServiceAccountCredentials.from_json_keyfile_name('con.json', scope)

gc = gspread.authorize(credentials)


wks = gc.open("temperatura")

ws = wks.get_worksheet(0)



ws.update_acell('A1','IT WORKS!')

我得到错误:

gc = gspread.authorize(credentials) AttributeError: 'module' object 没有“授权”属性

感谢反馈

问候 马林

【问题讨论】:

  • 你在那个目录中有一个名为gspread.py的脚本吗?
  • 不,我没有;/
  • 好的。这是此类问题的常见原因,但我想您的 Python 路径中可能还有另一个 gspread.py。您可以通过执行print(gspread.__file__) 来检查您是否获得了正确的gspread。这将向您显示模块的完整文件路径。此外,print(dir(gspread)) 将显示与gspread 相关的主要名称。
  • 我使用命令:sudo find / | grep gspread 和结果:/usr/local/lib/python2.7/dist-packages/gspread-0.1.0-py2.7.egg 查找:'/run/user/1000/gvfs':权限被拒绝
  • 对不起,我不知道还有什么建议。但希望您发布的额外信息能帮助熟悉gspread 的人解决您的问题。

标签: python raspberry-pi gspread


【解决方案1】:

您的代码似乎没问题。为什么不尝试重新安装 gspread 模块。

您可以在文件夹中创建一个虚拟环境并在那里安装所有必需的包。

这可能会帮助您在虚拟环境中运行所有项目,而不会影响其他模块或包。

$pip 安装虚拟环境

$ cd my_project_folder $ virtualenv my_project

$cd my_project $source bin/激活

现在您可以安装所有模块和包。

http://docs.python-guide.org/en/latest/dev/virtualenvs/

【讨论】:

  • 我做了同样的错误; AttributeError:“模块”对象没有属性“授权”
  • 安装 virtualenv 后,出现新错误:Traceback(最近一次调用最后一次):文件“test3.py”,第 12 行,在 wks = gc.open("temperatura").sheet1 文件" /home/pi/.local/lib/python2.7/site-packages/gspread/client.py”,第 89 行,在 open raise SpreadsheetNotFound gspread.exceptions.SpreadsheetNotFound Traceback(最后一次调用):文件“test3.py ",第 12 行,在 wks = gc.open("temperatura").sheet1 文件 "/home/pi/.local/lib/python2.7/site-packages/gspread/client.py",第 89 行,打开提出 SpreadsheetNotFound gspread.exceptions.SpreadsheetNotFound
  • 好的,这解决了我的问题,您必须记住转到 Google 表格并与您在 json_key['client_email'] 中的电子邮件共享您的电子表格。否则,您在尝试打开时会收到 SpreadsheetNotFound 异常。
猜你喜欢
  • 2010-11-18
  • 2018-08-28
相关资源
最近更新 更多