未找到“google-api-python-client”分布,应用程序需要 pyinstaller

问题描述 投票:0回答:10

我目前正在尝试使用 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: [email protected]
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 文件,其中包含项目中使用的所有库

任何帮助将不胜感激!

python pyinstaller google-api-python-client
10个回答
6
投票

实际上只是在 Windows 上遇到了这个问题,而 macOS 没问题。我正在使用 fbsPyQt5.

问题

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 或其他常见的打包框架

  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
    .
  3. 的任何地方)
  4. 尝试启动应用程序


6
投票

我能够在这里找到解决方案Link

将您的 google-api-python-client 版本更新为链接中指定的版本(为我工作)

还做了一个小.bat文件:

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

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


2
投票

我收到这个错误并且正在使用无服务器框架。要修复,我只是删除了 serverless.yml 中“自定义”块中的“slim:true”选项


1
投票

确保 pip 链接到 pip3 (Python 3) 而不是 pip2 (Python2)。在许多操作系统和发行版上,情况仍然如此。

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

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

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

例如

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

1
投票

我的案例是与 Pyinstaller --onefile 选项捆绑在一起的 Python-Flask Windows 应用程序,使用 .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'),     

.......],

1
投票

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


0
投票

如果您使用 PyCharm 进行编码,请执行以下操作:

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

0
投票

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


0
投票

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

一个适合我的班轮:

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... 文件夹。对于一个构建,路径是这样开始的:/../../..


0
投票

service = build('drive', 'v3', credentials=credentials, static_discovery=False) 适用于最新版本的 google api 客户端

LMaiorano 的回答

© www.soinside.com 2019 - 2024. All rights reserved.