1

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] No such file or directory: 'key.json'

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
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
Jerrod
  • 115
  • 12
  • 1
    Try putting the absolute path to your json file instead of the relative path (for example '~/keys/key.json' instead of just 'key.json') – wpercy Feb 28 '17 at 22:39

1 Answers1

7

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
)
thebjorn
  • 26,297
  • 11
  • 96
  • 138
  • Sorry, forgot to mention it is in the same directory, but that did it, thanks. – Jerrod Feb 28 '17 at 22:59
  • Good to hear. The preferred way to indicate that an answer solved your question is to click on the check mark below the score (left margin). If an answer helped you, you should click the up-arrow to give it points. This helps future users find answers that solved problems. – thebjorn Feb 28 '17 at 23:03