PyInstaller 无法在 linux 中构建 'Firebase Firestore python file'

PyInstaller can't build a 'Firebase Firestore python file' in linux

当我试图构建一个 linux 可执行文件时它构建成功但是当我尝试 运行 可执行文件时它显示这个错误

Traceback (most recent call last):
  File "pkgutil.py", line 637, in get_data
  File "/home/user/.local/lib/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 341, in get_data
    with open(path, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEI4f5JfX/grpc/_cython/_credentials/roots.pem'
Exception ignored in: 'grpc._cython.cygrpc.ssl_roots_override_callback'
Traceback (most recent call last):
  File "pkgutil.py", line 637, in get_data
  File "/home/user/.local/lib/python3.8/site-packages/PyInstaller/loader/pyimod03_importers.py", line 341, in get_data
    with open(path, 'rb') as fp:
FileNotFoundError: [Errno 2] No such file or directory: '/tmp/_MEI4f5JfX/grpc/_cython/_credentials/roots.pem'
E1122 03:58:17.060399901   23697 ssl_utils.cc:550]           assertion failed: pem_root_certs != nullptr
Aborted (core dumped)

python 文件 main.py

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db, firestore

# firebase app setup
cred = credentials.Certificate({ 
"type": "service_account",
"project_id": "project-216t8",
........
....
..
})
firebase_admin.initialize_app(cred)


def fetch_data():
    doc_ref = DocCollection_ref   
    docs= doc_ref.get()
    if docs.exists:
         docs = docs.to_dict()   
         print(f'Document data: {docs.to_dict()}')
    else:
     print(u'No such document!')

fetch_data()

这个解决方案对我有用

您将需要为依赖于 setuptools' get_distribution() 的包创建一个 pyinstaller 挂钩,以获取有关包的元数据信息。

在某个hooks 目录中创建一个hook 文件。文件名应为 hook-<modulename>.py 以便 pyinstaller 正确找到它(即 hook-gcloud.py)。

解决方案: 创建两个 hook 文件:

  • 勾-gcloud.py

  • 勾-grpc.py

hook-gcloud.py中写下代码并保存:

from PyInstaller.utils.hooks import copy_metadata
datas = copy_metadata('gcloud')

hook-grpc.py中写下代码并保存:

from PyInstaller.utils.hooks import collect_data_files
datas = collect_data_files ( 'grpc' )

现在将这两个文件复制并粘贴到 /lib/python3.8/site-packages/PyInstaller/hooks/ 目录。