首页 > 解决方案 > Pyinstaller 不包括 google-api-python-client

问题描述

我已经编写了一个 Kivy 应用程序,现在我正在尝试使用 PyInstaller 为 MacOS 打包它。它使用 Google Calendar API,所以我需要使用 google-api-python-client 模块。当我按照 Kivy 打包说明打包应用时,没有找到 google-api-python-client。我收到了这个错误:

[INFO   ] [Logger      ] Record log in /_____/.kivy/logs/kivy_20-06-23_6.txt
[INFO   ] [Kivy        ] v1.11.1
[INFO   ] [Kivy        ] Installed at "/____.app/Contents/MacOS/kivy/__init__.pyc"
[INFO   ] [Python      ] v3.7.4 (v3.7.4:e09359112e, Jul  8 2019, 14:54:52) 
[Clang 6.0 (clang-600.0.57)]
[INFO   ] [Python      ] Interpreter at "/____/Contents/MacOS/___"
[INFO   ] [Factory     ] 184 symbols loaded
[INFO   ] [Image       ] Providers: img_tex, img_imageio, img_dds, img_sdl2, img_gif (img_pil, img_ffpyplayer ignored)
[INFO   ] [Text        ] Provider: sdl2
[INFO   ] [Window      ] Provider: sdl2
[INFO   ] [GL          ] Using the "OpenGL ES 2" graphics system
[INFO   ] [GL          ] Backend used <sdl2>
[INFO   ] [GL          ] OpenGL version <b'2.1 INTEL-14.5.22'>
[INFO   ] [GL          ] OpenGL vendor <b'Intel Inc.'>
[INFO   ] [GL          ] OpenGL renderer <b'Intel Iris Pro OpenGL Engine'>
[INFO   ] [GL          ] OpenGL parsed version: 2, 1
[INFO   ] [GL          ] Shading version <b'1.20'>
[INFO   ] [GL          ] Texture max size <16384>
[INFO   ] [GL          ] Texture max units <16>
[INFO   ] [Window      ] auto add sdl2 input provider
[INFO   ] [Window      ] virtual keyboard not allowed, single mode, not docked
 Traceback (most recent call last):
   File "main.py", line 11, in <module>
   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
   File "/the_venv_directory/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
     exec(bytecode, module.__dict__)
   File "googleapiclient/discovery.py", line 67, in <module>
   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
   File "/the_venv_directory/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
     exec(bytecode, module.__dict__)
   File "googleapiclient/http.py", line 67, in <module>
   File "<frozen importlib._bootstrap>", line 983, in _find_and_load
   File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
   File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
   File "/the_venv_directory/lib/python3.7/site-packages/PyInstaller/loader/pyimod03_importers.py", line 623, in exec_module
     exec(bytecode, module.__dict__)
   File "googleapiclient/model.py", line 36, in <module>
   File "pkg_resources/__init__.py", line 481, in get_distribution
   File "pkg_resources/__init__.py", line 357, in get_provider
   File "pkg_resources/__init__.py", line 900, in require
   File "pkg_resources/__init__.py", line 786, in resolve
 pkg_resources.DistributionNotFound: The 'google-api-python-client' distribution was not found and is required by the application
[64310] Failed to execute script main
logout

这是我的规格文件:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['main.py'],
             pathex=['Project Parent Directory'],
             binaries=[],
             datas=[],
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=['_tkinter', 'Tkinter', 'enchant', 'twisted'],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          [],
          exclude_binaries=True,
          name='___',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          console=False , icon='Icon.icns')
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               upx_exclude=[],
               name='___')
app = BUNDLE(coll,
             name='___.app',
             icon='Icon.icns',
             bundle_identifier="com.___.talkingcalendar")

我看过这篇文章,但答案不适用于 PyInstaller。谢谢你的帮助!

标签: python-3.xkivypyinstallergoogle-api-python-client

解决方案


答案是从site-packages被调用的文件夹中复制一个文件夹google_api_python_client-x.y.z.dist-info。这个文件夹应该被复制到*.app/Contents/MacOS/. 但后来我发现正确的方法是将这个元组放入datas=[*]*.spec 文件的行中:('/path/to/lib/site-packages/google_api_python_client-x.y.z.dist-info/*', 'google_api_python_client-x.y.z.dist-info').


推荐阅读