Django无法识别或看到JSON文件 [英] Django not recognizing or seeing JSON file

查看:106
本文介绍了Django无法识别或看到JSON文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试将django与Google表格集成在一起,我正在尝试使用gspread.我可以使用python filename.py 来查看数据,但是当我运行 python manage.py runserver 时,却不断出现此错误:

I've been working on trying to integrate google sheets with django, i'm trying to use gspread. I can see the data using python filename.py, but when I run python manage.py runserver, I keep getting this error:

IOError:[Errno 2]没有这样的文件或目录:'key.json'

IOError: [Errno 2] No such file or directory: 'key.json'

由于某种原因无法识别我的json文件,我也尝试使用不带.json的'key',但不走运.我一直在这里搜索,这里有什么想法吗?这是我的下面的代码

It's not recognizing for seeing my json file for some reason, i've also tried using 'key' without the .json, no luck. I've been googling here, any ideas here? Here's my code below

****************************下面的代码*******************************

*************************** code below *******************************

import gspread
import json
from oauth2client.service_account import ServiceAccountCredentials
import os

scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('key.json', scope)

gc = gspread.authorize(credentials)
wks = gc.open("RAMP - Master").sheet1
print wks

cell_list = wks.range('A1:B7')
print cell_list

推荐答案

如果 key.json 与您正在运行的文件位于同一目录中,则正确的语法为:

If key.json is in the same directory as the file you're running, then the correct syntax is:

import os
DIRNAME = os.path.dirname(__file__)
credentials = ServiceAccountCredentials.from_json_keyfile_name(
    os.path.join(DIRNAME, 'key.json'),
    scope
)

这篇关于Django无法识别或看到JSON文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆