首页 > 解决方案 > Python google 电子表格代码从命令行运行,但不是从 Web 脚本运行

问题描述

我正在尝试从 python 访问 google 工作表,并且在从命令行运行时工作正常。

#!C:/Users/uwe/AppData/Local/Programs/Python/Python37-32/python
print('Content-type: text/html\r\n')

import gspread 
from oauth2client.service_account import ServiceAccountCredentials

尝试从本地主机运行它时出现错误

    =>   18 import gspread 
     19 from oauth2client.service_account import ServiceAccountCredentials
     20 
gspread undefined
 C:\Users\uwe\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\__init__.py in <module>()
     14 
     15 
=>   16 from .auth import oauth, service_account, service_account_from_dict
     17 from .client import Client
     18 from .models import Spreadsheet, Worksheet, Cell
oauth undefined, service_account undefined, service_account_from_dict undefined
 C:\Users\uwe\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\auth.py in <module>()
     45 
     46 
=>   47 DEFAULT_CONFIG_DIR = get_config_dir()
     48 
     49 DEFAULT_CREDENTIALS_FILENAME = os.path.join(
DEFAULT_CONFIG_DIR undefined, get_config_dir = <function get_config_dir>
 C:\Users\uwe\AppData\Local\Programs\Python\Python37-32\lib\site-packages\gspread\auth.py in get_config_dir(config_dir_name='gspread', os_is_windows=True)
     38     """
     39     if os_is_windows:
=>   40         return os.path.join(os.environ["APPDATA"], config_dir_name)
     41     else:
     42         return os.path.join(
global os = <module 'os' from 'C:\\Users\\uwe\\AppData\\Local\\Programs\\Python\\Python37-32\\lib\\os.py'>, os.path = <module 'ntpath' from 'C:\\Users\\uwe\\AppData\\...\\Programs\\Python\\Python37-32\\lib\\ntpath.py'>, os.path.join = <function join>, os.environ = environ({'HTTP_HOST': 'localhost', 'HTTP_CONNECT...readsheet.py', 'SCRIPT_NAME': '/spreadsheet.py'}), config_dir_name = 'gspread'
 C:\Users\uwe\AppData\Local\Programs\Python\Python37-32\lib\os.py in __getitem__(self=environ({'HTTP_HOST': 'localhost', 'HTTP_CONNECT...readsheet.py', 'SCRIPT_NAME': '/spreadsheet.py'}), key='APPDATA')
    676         except KeyError:
    677             # raise KeyError with the original key value
=>  678             raise KeyError(key) from None
    679         return self.decodevalue(value)
    680 
builtin KeyError = <class 'KeyError'>, key = 'APPDATA'
KeyError: 'APPDATA'
      args = ('APPDATA',)
      with_traceback = <built-in method with_traceback of KeyError object>

我错过了什么?(Python新手,Win10,Apache2.4)

标签: pythongoogle-sheets

解决方案


推荐阅读