title: Google API: sheets KeyError: '_module' while calling get_credentials()
body: <p>I have pretty similar code as google on a quick start page <a href="https://developers.google.com/sheets/api/quickstart/python" rel="nofollow noreferrer">suggest</a>.</p>
  
  <pre><code>from __future__ import print_function
  import httplib2
  import os
  
  from apiclient import discovery
  from oauth2client import client
  from oauth2client import tools
  from oauth2client.file import Storage
  
  try:
      import argparse
      flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
  except ImportError:
      flags = None
  
  # If modifying these scopes, delete your previously saved credentials
  # at ~/.credentials/sheets.googleapis.com-python-quickstart.json
  SCOPES = 'https://www.googleapis.com/auth/spreadsheets.readonly'
  CLIENT_SECRET_FILE = 'client_secret.json'
  APPLICATION_NAME = 'Google Sheets API Python Quickstart'
  
  
  def get_credentials():
      """Gets valid user credentials from storage.
  
      If nothing has been stored, or if the stored credentials are invalid,
      the OAuth2 flow is completed to obtain the new credentials.
  
      Returns:
          Credentials, the obtained credential.
      """
      home_dir = os.path.expanduser('~')
      credential_dir = os.path.join(home_dir, '.credentials')
      if not os.path.exists(credential_dir):
          os.makedirs(credential_dir)
      credential_path = os.path.join(credential_dir,
                                     'sheets.googleapis.com-python-quickstart.json')
  
      store = Storage(credential_path)
      credentials = store.get()
      if not credentials or credentials.invalid:
          flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
          flow.user_agent = APPLICATION_NAME
          if flags:
              credentials = tools.run_flow(flow, store, flags)
          else: # Needed only for compatibility with Python 2.6
              credentials = tools.run(flow, store)
          print('Storing credentials to ' + credential_path)
      return credentials
  
  def main():
      """Shows basic usage of the Sheets API.
  
      Creates a Sheets API service object and prints the names and majors of
      students in a sample spreadsheet:
      https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
      """
      credentials = get_credentials()
      http = credentials.authorize(httplib2.Http())
      discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
                      'version=v4')
      service = discovery.build('sheets', 'v4', http=http,
                                discoveryServiceUrl=discoveryUrl)
  
      spreadsheetId = '1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms'
      rangeName = 'Class Data!A2:E'
      result = service.spreadsheets().values().get(
          spreadsheetId=spreadsheetId, range=rangeName).execute()
      values = result.get('values', [])
  
      if not values:
          print('No data found.')
      else:
          print('Name, Major:')
          for row in values:
              # Print columns A and E, which correspond to indices 0 and 4.
              print('%s, %s' % (row[0], row[4]))
  
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Firstly, I created my own secret file. Then executed without that file 'sheets.googleapis.com-python-quickstart.json' and <code>.credentials</code> folder was created, but  faced <code>sheets.googleapis.com-python-quickstart.json: No such file or directory</code> error</p>
  
  <p>So then I created that empty file <code>sheets.googleapis.com-python-quickstart.json</code> on that folder, but faced another error <code>KeyError: '_module'</code>. I even tried to rename my secret file as <code>sheets.googleapis.com-python-quickstart.json</code> - same error. I have executed this code from console and from ipython notebook - same error.  Changing SCOPES by deleting <code>.readonly</code> - didn't help</p>
  
  <p>Is there any webpage should be opened? What am I doing wrong?</p>
  
  <p>My question looks the same <a href="https://stackoverflow.com/questions/30966906/google-api-quickstart-py-error-keyerror-module">as</a>  - but nothing helped and my question should be about a bit different think </p>
  
  <p>Full error stack:</p>
  
  <pre><code>---------------------------------------------------------------------------
  KeyError                                  Traceback (most recent call last)
  &lt;ipython-input-83-58ca95c5b364&gt; in &lt;module&gt;()
  ----&gt; 1 main()
  
  &lt;ipython-input-82-1e078df2aa91&gt; in main()
       37     https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit
       38     """
  ---&gt; 39     credentials = get_credentials()
       40     http = credentials.authorize(httplib2.Http())
       41     discoveryUrl = ('https://sheets.googleapis.com/$discovery/rest?'
  
  &lt;ipython-input-82-1e078df2aa91&gt; in get_credentials()
       20 
       21     store = Storage(credential_path)
  ---&gt; 22     credentials = store.get()
       23 
       24     if not credentials or credentials.invalid:
  
  ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\oauth2client\client.py in get(self)
      405         self.acquire_lock()
      406         try:
  --&gt; 407             return self.locked_get()
      408         finally:
      409             self.release_lock()
  
  ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\oauth2client\file.py in locked_get(self)
       52 
       53         try:
  ---&gt; 54             credentials = client.Credentials.new_from_json(content)
       55             credentials.set_store(self)
       56         except ValueError:
  
  ~\AppData\Local\Continuum\Anaconda3\lib\site-packages\oauth2client\client.py in new_from_json(cls, json_data)
      300         # Find and call the right classmethod from_json() to restore
      301         # the object.
  --&gt; 302         module_name = data['_module']
      303         try:
      304             module_obj = __import__(module_name)
  
  KeyError: '_module'
  </code></pre>