未找到 'google-api-python-client' 分发版,但该分发版是带有 pyinstaller 的应用程序所必需的

The 'google-api-python-client' distribution was not found and is required by the application with pyinstaller

我目前正在尝试使用 pyinstaller 构建应用程序。我遇到了错误 The 'google-api-python-client' distribution was not found and is required by the application,我完全不知道为什么。

运行 pip show google-api-python-client 结果

Name: google-api-python-client
Version: 1.8.2
Summary: Google API Client Library for Python
Home-page: http://github.com/google/google-api-python-client/
Author: Google LLC
Author-email: googleapis-packages@google.com
License: Apache 2.0
Location: c:\dev\software\schoology_scrape\schoology_scrape_venv\lib\site-packages
Requires: google-auth-httplib2, uritemplate, google-auth, google-api-core, httplib2, six
Required-by:

我还有一个 requirements.txt 文件,其中包含项目中使用的所有库

如有任何帮助,我们将不胜感激!

在 windows 上只是 运行 解决了这个问题,而 macOS 没问题。我正在用 fbs and PyQt5.

构建

问题

google-api-python-client 不是 python 模块,而是资源,这意味着您不能将其作为隐藏导入注入。 googleapiclient.modelgoogle-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 或其他常见的打包框架

  1. 找到 google_api_python_client-*/
    • 可能在某个地方<pythonInstallLocation>/lib/site-packages/
  2. 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 - 没有自动打包挂钩(未测试):

如果您的打包解决方案没有上述类似的钩子,那么:

  1. 构建您的应用程序
  2. 手动将 google_api_python_client-*/ 文件夹从 <pythonInstallLocation>/lib/site-packages/ 复制到内置应用程序的目录中(或编译的 python 脚本试图访问 google-api-python-client.[=65= 的任何地方) ]
  3. 尝试启动应用程序

确保 pip 链接到 pip3 (Python 3) 而不是 pip2 (Python2)。在许多 OS(es) 和发行版中,情况仍然如此。

检查是否解决了您的问题:

python3 -m pip install --upgrade google-api-python-client

如果是,则向您的 .bashrc 添加别名,将 pip 链接到 pip3 而不是 pip2。

例如

echo "alias pip='pip3'" >> ~/.bashrc

如果您使用 PyCharm 进行编码,请执行下一步:

  1. 运行 $ pip3 freeze 在终端
  2. 在您的项目文件夹中创建文件“requirements.txt”
  3. 将第一步的结果复制到“requirements.txt”
  4. PyCharm 将显示 /venv 中安装的软件包不满足“requirements.txt”的消息。单击“安装包”按钮,等待所有包安装完毕,然后重新构建您的应用程序。

只是想添加到@joeyipanimation 的回答中,因为它帮助我在下班后解决了问题。在你的exe文件夹或库区搜索google_api_python_client-1.9.3.dist-info,复制到exe所在的主文件夹下。

我的案例是 Python-Flask windows 应用程序与 Pyinstaller --onefile 选项捆绑在一起,使用 .spec 文件。

我已将文件夹 google_api_python_client-1.9.3.dist-info 从原始位置(可能是 windows site-packages 文件夹)复制到项目文件夹。

将以下行添加到 Pyinstaller 规范文件数据部分 (app.spec) 是问题的解决方案。

a = Analysis(.......  

datas=[.....

('project\google_api_python_client-1.9.3.dist-info','google_api_python_client-1.9.3.dist-info'),     

.......],

我在这里找到了解决方案Link

将您的 google-api-python-客户端版本更新为 link 中指定的版本(为我工作)

还制作了一个小.bat文件:

pyinstaller --hidden-import="pkg_resources.py2_warn" --hidden-import="googleapiclient" --hidden-import="apiclient"  main.py --onefile

另请注意:我运行虚拟环境中的bat文件。

将 Google 目录从 Python 应用程序安装位置的 Python//Lib/site-packages 目录复制到由 pyinstaller 创建的 dist/ 目录.

我已经在 中发布了详细的答案,但是这个问题是我掉进兔子洞的问题,所以提供一些反馈是公平的。

一款适合我的衬垫:

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

SRC 是相对路径形式,您必须从当前工作目录中找到 google_api_python_client-x.y.z.dist-info 文件夹。所以如果你使用 venv,这意味着它应该是这样的:

--add-data venv/Lib/site-packages/google_api_python_client-x.y.z.dist-info;google_api_python_client-x.y.z.dist-info

如果您使用 conda 作为环境,那么您必须回滚,直到在该环境中找到相同的 google_api_python_client... 文件夹。对于一个构建,路径是这样开始的:/../../..

我收到此错误并且正在使用无服务器框架。要修复,我只是删除了 serverless.yml

中“自定义”块中的“slim:true”选项