gpt4 book ai didi

python - 如何在Pyinstaller中包含Google API JSON凭证文件?

转载 作者:行者123 更新时间:2023-12-02 10:42:02 25 4
gpt4 key购买 nike

我的第一个问题在这里。我是业余爱好者自学的python。我知道这可能是一个愚蠢的问题,但是经过数天的搜索,我找不到任何信息。

我正在制作一个应用程序,以使用Google API读取google电子表格中的数据,然后使用PYFPDF输出带有预格式化布局的漂亮图片的PDF文件。在Python3.5.2 + Pycharm + Mac OSX环境下,它可以在我的计算机上正常运行。

我的应用首先要获得访问Google API的凭据。

scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('my_app_creds.json', scope)
client = gspread.authorize(creds)

my_app_creds.json文件如下所示:
{
"type": "service_account",
"project_id": "my_app",
"private_key_id": "1234567890",
"private_key": "-----BEGIN PRIVATE KEY-----\nVERY_LONG_PRIVATE_KEY\n-----END PRIVATE KEY-----\n",
"client_email": "my_email@my_app.iam.gserviceaccount.com",
"client_id": "0987654321",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/my_app.iam.gserviceaccount.com"
}

当我使用pyinstaller编译软件包以-F(onefolder)模式制作可执行文件时,事情会出错。应用程序文件夹已成功创建,并且正确位于“dist”文件夹中。但是,当我尝试执行文件时,它会抛出错误:
FileNotFoundError: [Errno 2] No such file or directory: 'my_app_creds.json'
[24713] Failed to execute script my_app

我已经将my_app_creds.json包含到a.datas的.spec文件中,并且我使用了pyinstaller文档中的命令:
pyinstaller my_app.spec

my_app.spec是包含“找不到”的.json文件
a = Analysis(['my_app.py'],
pathex=['/Users/gungpetrucci/PycharmProjects/myapp'],
binaries=[],
datas=[('my_app_creds.json', '.')],

我确认my_app_creds.json与'dist/my_app'中的可执行文件位于同一文件夹中。我不知道为什么可执行文件找不到?

当我尝试搜索论坛时,对我来说似乎还不清楚:
  • 我不确定_MEIPASS与这有关吗?但是我认为_MEIPASS仅在pyinstaller中使用-F onefile模式会引起关注?
  • 由于已编译的应用程序永远不会运行通过凭据进程,因此它永远不会进入图像处理进程。我想知道应用程序能否在子文件夹中(编译之前)找到.png图片吗?如何将整个图像文件夹包含到dist包中并使其可访问?

  • 请帮忙..

    最佳答案

    在我的一个项目中,我遇到了同样的问题。我使用以下解决方法-

  • 创建“my_app_creds.json”中的内容字典:
    app_creds_dictionary = <paste contents of json file including {}>
  • 使用“from_json_keyfile_dict()”函数创建凭据而不是文件:
    creds = ServiceAccountCredentials.from_json_keyfile_dict(app_creds_dictionary, scope)
  • 现在,由于这是python文件本身的一部分,因此无需担心粘贴/维护json文件的副本。即使以这种方式创建的.exe也对我有用!

  • 希望这可以帮助 !

    关于python - 如何在Pyinstaller中包含Google API JSON凭证文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48435699/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com