实际上只是在 windows 上遇到了这个问题,而 macOS 还可以。我正在使用 fbs 和 PyQt5 构建。
问题
google-api-python-client 不是 python 模块,而是资源,这意味着您不能将其作为隐藏导入注入。 googleapiclient.model 从google-api-python-client 文件夹中读取分发信息作为打包资源。
你的完整错误可能看起来更接近这个:
...
File "c:\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\googleapiclient\http.py", line 67, in <module>
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "c:\python36\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 627, in exec_module
exec(bytecode, module.__dict__)
File "site-packages\googleapiclient\model.py", line 36, in <module>
File "site-packages\pkg_resources\__init__.py", line 479, in get_distribution
File "site-packages\pkg_resources\__init__.py", line 355, in get_provider
File "site-packages\pkg_resources\__init__.py", line 898, in require
File "site-packages\pkg_resources\__init__.py", line 784, in resolve
pkg_resources.DistributionNotFound: The 'google-api-python-client' distribution was not found and is required by the application
解决方案 1 - 如果使用 fbs 或其他常见的打包框架
- 找到
google_api_python_client-*/
- 可能在某处
<pythonInstallLocation>/lib/site-packages/
- 将
google_api_python_client-*/ 复制到应用程序的src 资源目录中。对于fbs,这可以是:
-
src/freeze/windows/(推荐),或
src/resources/windows/
现在,当您 fbs freeze 和随后 fbs installer 您的应用程序时,google_api_python_client-*/ 将与其他 googleapiclient python 库一起包含在构建的应用程序的目录中,并且错误应该会消失。
See: fbs project directory structure
解决方案 2 - 没有自动打包挂钩(未经测试):
如果你的打包方案没有上面类似的钩子,那么:
- 构建您的应用
- 手动将
google_api_python_client-*/ 文件夹从<pythonInstallLocation>/lib/site-packages/ 复制到已构建应用的目录(或您编译的python 脚本试图访问google-api-python-client 的任何位置。
- 尝试启动应用程序
pythonfbsfreezegoogle-api-python-client