Pyinstaller 不包括 google-api-python-client

Pyinstaller not including google-api-python-client

我已经编写了一个 Kivy 应用程序,我现在正尝试使用 PyInstaller 为 MacOS 打包它。它使用 Google 日历 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")

我看过 post,但答案不适用于 PyInstaller。感谢您的帮助!

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

另一个可以在一行中解决您的问题的答案,希望是添加带有 --add-data 参数的文件夹。 Relevant link and also this.

pyinstaller [.py_name] -n [.exe_name] --onefile --add-data [SRC:DEST]

你的情况--add-data '/path/to/lib/site-packages/google_api_python_client-x.y.z.dist-info:google_api_python_client-x.y.z.dist-info'

我之前遇到的几个问题:

  • SRC部分不能用绝对路径写,应该是相对路径

  • 路径取自当前工作文件夹。对于我早期的构建之一,我需要使用 ../../ 返回几个文件夹,因此如果您要添加的文件不在当前工作文件夹中,您可以这样做。

  • 路径的引号,很长一段时间我不需要使用它们,事实上,它们导致了错误。但是在上个月,我猜是因为pyinstaller的一些更新,我需要再次添加引号。

  • 在 SRC 和 DEST 之间可能是 ';'或 ':',我正在使用 ';'对于 Windows,所以对于 macOS 可能是“:”。

你链接的问题有一个不同的答案,它链接了一个 GitHub 页面和详细信息 here 在那里我也分享了我之前的发现或多或少归结为上述方法和解释.