Python Google Sheet API:googleapiclient.errors.UnknownApiNameOrVersion:名称:工作表版本:v4

问题描述 投票:0回答:2

我编写了一些 .py 文件来访问和写入我的谷歌表上的数据,并使用 pyinstaller 将这些文件打包到 .exe 中。但是当我将.py打包成.exe后,我就无法再执行它了

一直显示此错误:

 File "GoogleSheet.py", line 9, in __init__
  File "Google_Process.py", line 31, in __init__
  File "googleapiclient\_helpers.py", line 130, in positional_wrapper
  File "googleapiclient\discovery.py", line 287, in build
  File "googleapiclient\discovery.py", line 404, in _retrieve_discovery_doc
googleapiclient.errors.UnknownApiNameOrVersion: name: sheets  version: v4

我已经尝试过方法:https://github.com/nithinmurali/pygsheets/issues/490

但我不确定如何修改我的代码,或者这个方法是否可以修复我的错误。

这是我的.py:

from Google_Process import GoogleAPIClient
import pandas as pd

class GoogleSheets(GoogleAPIClient):
    def __init__(self, status = 'DONE', order_number = '') -> None:
        self.status = status
        self.order_number = order_number
        # Call GoogleAPIClient.__init__(),give serviceName, version, scope
        super().__init__(
            'sheets',
            'v4',
            ['https://www.googleapis.com/auth/spreadsheets'],
        )

    def appendWorksheet(self):
        if self.status == 'DONE':
            df=pd.DataFrame(
            {'Order': [self.order_number],
            'Status': ['DONE'],
            'Mark': ['-']}
            )
            self.googleAPIService.spreadsheets().values().append(
                spreadsheetId='<ID>',
                range='sheet1',
                valueInputOption='USER_ENTERED',
                body={
                    'majorDimension': 'ROWS',
                    'values': df.values.tolist()
                },
            ).execute()
            return

这是另一个文件:

import os.path
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
from google_auth_oauthlib.flow import InstalledAppFlow
from googleapiclient.discovery import build

class GoogleAPIClient:
    SECRET_PATH = '.\\PackingElf\\.credentials\\client_secrets_file.json'
    CREDS_PATH = '.\\PackingElf\\.credentials\\cred.json'
    
    def __init__(self, serviceName: str, version: str, scopes: list) -> None:
        self.creds = None
        # The file client_secret.json stores the user's access and refresh tokens, and is
        # created automatically when the authorization flow completes for the first
        # time.

        if os.path.exists(self.CREDS_PATH):
            self.creds = Credentials.from_authorized_user_file(self.CREDS_PATH, scopes)

        # If there are no (valid) credentials available, let the user log in.
        if not self.creds or not self.creds.valid:
            if self.creds and self.creds.expired and self.creds.refresh_token:
                self.creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(
                    self.SECRET_PATH, scopes)
                self.creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open(self.CREDS_PATH, 'w') as token:
                token.write(self.creds.to_json())
            
            self.googleAPIService = build(serviceName, version, credentials=self.creds)

if __name__ == '__main__':
    googleSheetAPI = GoogleAPIClient(
        'sheets',
        'v4',
        ['https://www.googleapis.com/auth/spreadsheets'],
        )

    print(googleSheetAPI.googleAPIService)

有什么办法可以解决这个问题吗?请

我使用 vscode 作为 IDE

python pyinstaller google-sheets-api
2个回答
0
投票

我在创建一个读取和写入谷歌电子表格的库存应用程序时刚刚遇到了这个问题。我尝试了大约 4 种在网上看到的不同解决方案,但都没有成功。我将把您重定向到 github 上的这个线程thread。看来这是一个持续存在的问题。


0
投票

尝试以下操作:

  self.googleAPIService = build(serviceName, version, credentials=self.creds, static_discovery=False)
© www.soinside.com 2019 - 2024. All rights reserved.