我想要达到的目标.
按下按钮从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相关的内容的话,还是可以的。
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的代码
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代码
import requests
BASE = 'https://username.pythonanywhere.com/callApi/test.py'
response = requests.get(BASE)
print(response.json())
main.py输出
{'data': 54}
Test.py代码
a = 20
b = 34
total = a+b
print(total)
问题是
如果当时请求runMe2.py,则会出现此错误。检查上面的runMe2.py代码
app.py托管在https://www.pythonanywhere.com/上
ModuleNotFoundError: No module named 'gspread'
但是,我在pythonanywhere上安装了using为什么使用命令。但这不管用。
发布于 2021-04-10 18:26:07
您要么没有在当前python环境中安装gspread
包,要么安装在某个地方(例如,在diff中)。你的脚本找不到它。
尝试使用pip3在运行脚本的环境中安装包:
pip3 install gspread
发布于 2021-04-10 19:15:02
您可以在Windows上尝试这样的方法
pip install gspread
或者在Mac上
pip3 install gspread
如果您在Docker上运行,或者使用requirements.txt构建,您可以尝试添加这一行您的requirements.txt文件
gspread==3.7.0
有关此包的任何其他说明,请参见=> https://github.com/burnash/gspread。
发布于 2021-04-11 06:01:49
从上面的链接解压缩文件下载tar文件:gscide-3.7.0.tar.gz并转换zip中的文件夹,然后将其上传到服务器上。
打开bash控制台并使用命令作为
$ unzip gspread-3.7.0
$ cd gspread-3.7.0
$ python3.7 setup.py install --user
https://stackoverflow.com/questions/67034928
复制相似问题