2

代码在 Pycharm 中完美运行,或者在运行 .py 文件时运行,但我需要将应用程序作为 .exe 文件才能在没有 python 的设备上运行。

我试图允许用户从 tkinter 窗口报告应用程序中的错误/提供反馈。然后通过 gmail-api 将反馈发送给我。

.exe 文件由 pyinstaller 制作(从虚拟环境内部运行)运行 exe 文件时一切正常,直到:

service = build(serviceName='gmail',
                        version='v1',
                        credentials=creds,
                        discoveryServiceUrl="https://gmail.googleapis.com/$discovery/rest?version=v1")

它在哪里生产

  File "googleapiclient\_helpers.py", line 134, in positional_wrapper
  File "googleapiclient\discovery.py", line 273, in build
  File "googleapiclient\discovery.py", line 387, in _retrieve_discovery_doc
googleapiclient.errors.UnknownApiNameOrVersion: name: gmail  version: v1

单击 tkinter 按钮时,将执行以下代码。

gmail 代码几乎完全复制自 google gmail api 示例。

小代码片段:

from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
from google.oauth2.credentials import Credentials
SCOPES = ['https://www.googleapis.com/auth/gmail.send']
def process_report():
        creds = None
        if os.path.exists('token.json'):
            creds = Credentials.from_authorized_user_file('token.json', SCOPES)
        # If there are no (valid) credentials available, let the user log in.
        if not creds or not creds.valid:
            if creds and creds.expired and creds.refresh_token:
                creds.refresh(Request())
            else:
                flow = InstalledAppFlow.from_client_secrets_file(
                    'client_id.json', SCOPES)
                creds = flow.run_local_server(port=0)
            # Save the credentials for the next run
            with open('token.json', 'w') as token:
                token.write(creds.to_json())

        service = build(serviceName='gmail',
                        version='v1',
                        credentials=creds,
                        discoveryServiceUrl="https://gmail.googleapis.com/$discovery/rest?version=v1")

        msg = create_message("sender_email@gmail.com",
                       "reciever_email@other.com",
                       subject, message)

        send_message(service, "me", msg)

非常感谢任何帮助或建议。

4

2 回答 2

2

通过将 google-api-python-client 恢复为 1.8.0 解决了该问题

pip install google-api-python-client==1.8.0

于 2021-04-01T04:54:10.857 回答
1

与谷歌课堂 API 有完全相同的问题。作为 .py 工作,使用 pyinstaller 转换时不能作为 .exe 工作。完全相同的错误,除了 Google 课堂 API。

完全相同的解决方案有效(恢复 google-api-python-client)。我会发表评论,但我没有足够的评论点。

于 2021-05-07T04:14:47.677 回答