首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ModuleNotFoundError:在python上没有名为“gspread”的模块

ModuleNotFoundError:在python上没有名为“gspread”的模块
EN

Stack Overflow用户
提问于 2021-04-10 13:32:34
回答 3查看 1K关注 0票数 1

我想要达到的目标.

按下按钮从google运行保存在pythonanywhere主机上的python脚本。

检查达斯汀·米歇尔的答案

每个文件的任务?

app.py:包含使用烧瓶制作的REST代码。

runMe.py:包含获取值的代码(google单元格A1:A2)。和和两个值都将sum发送回A3。

main.py:包含GET请求的代码,如果用户希望运行另一个文件,则名称(runMe.py).filename可能会更改。

我通过使用Flask.it制作了一个API,它可以很好地在线和离线工作,但是如果您想推荐任何与app.py.代码评审App.py相关的内容的话,还是可以的。

代码语言:javascript
运行
复制
from flask import Flask, jsonify
from flask_restful import Api, Resource
import os

app = Flask(__name__)
api = Api(app)


class callApi(Resource):
    def get(self, file_name):
        my_dir = os.path.dirname(__file__)
        file_path = os.path.join(my_dir, file_name)
        file = open(file_path)
        getvalues = {}
        exec(file.read(), getvalues)
        return jsonify({'data': getvalues['total']})


api.add_resource(callApi, "/callApi/<string:file_name>")

if __name__ == '__main__':
    app.run()

以下是runMe2.py的代码

代码语言:javascript
运行
复制
import gspread
from oauth2client.service_account import ServiceAccountCredentials

# use creds to create a client to interact with the Google Drive API

scopes =['https://www.googleapis.com/auth/spreadsheets',"https://www.googleapis.com/auth/drive.file","https://www.googleapis.com/auth/drive"]

creds = ServiceAccountCredentials.from_json_keyfile_name('service_account.json', scopes)
client = gspread.authorize(creds)

# Find a workbook by name and open the first sheet
# Make sure you use the right name here.
sheet = client.open("Demosheet").sheet1


# Extract and print all of the values
list_of_hashes = sheet.get_all_records()
print(list_of_hashes)

下面是main.py代码

代码语言:javascript
运行
复制
import requests

BASE = 'https://username.pythonanywhere.com/callApi/test.py'

response = requests.get(BASE)
print(response.json())

main.py输出

代码语言:javascript
运行
复制
{'data': 54}

Test.py代码

代码语言:javascript
运行
复制
a = 20
b = 34
total = a+b
print(total)

问题是

如果当时请求runMe2.py,则会出现此错误。检查上面的runMe2.py代码

app.py托管在https://www.pythonanywhere.com/

代码语言:javascript
运行
复制
ModuleNotFoundError: No module named 'gspread'

但是,我在pythonanywhere上安装了using为什么使用命令。但这不管用。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-04-10 18:26:07

您要么没有在当前python环境中安装gspread包,要么安装在某个地方(例如,在diff中)。你的脚本找不到它。

尝试使用pip3在运行脚本的环境中安装包:

代码语言:javascript
运行
复制
pip3 install gspread
票数 2
EN

Stack Overflow用户

发布于 2021-04-10 19:15:02

您可以在Windows上尝试这样的方法

代码语言:javascript
运行
复制
pip install gspread

或者在Mac上

代码语言:javascript
运行
复制
pip3 install gspread

如果您在Docker上运行,或者使用requirements.txt构建,您可以尝试添加这一行您的requirements.txt文件

代码语言:javascript
运行
复制
gspread==3.7.0

有关此包的任何其他说明,请参见=> https://github.com/burnash/gspread

票数 0
EN

Stack Overflow用户

发布于 2021-04-11 06:01:49

在这里下载扩展

从上面的链接解压缩文件下载tar文件:gscide-3.7.0.tar.gz并转换zip中的文件夹,然后将其上传到服务器上。

打开bash控制台并使用命令作为

代码语言:javascript
运行
复制
$ unzip gspread-3.7.0
$ cd gspread-3.7.0
$ python3.7 setup.py install --user
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67034928

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档