【问题标题】:The 'google-api-python-client' distribution was not found and is required by the application with pyinstaller未找到“google-api-python-client”分发版,它是带有 pyinstaller 的应用程序所必需的
【发布时间】:2020-04-29 20:16:40
【问题描述】:

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

任何帮助将不胜感激!

【问题讨论】:

    标签: python pyinstaller google-api-python-client


    【解决方案1】:

    我可以在这里找到解决方案Link

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

    还做了一个小.bat文件:

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

    另外值得注意的是:我在虚拟环境中运行了 bat 文件。

    【讨论】:

    • 我可以确认这个灵魂有效!我将 google-api-python-client 降级如下: pip install google-api-python-client==1.8.0
    • 感谢 @ENDEESA 的卸载命令。我有 google-api-python-client 1.12.8 并尝试了几个不起作用的修复程序。降级到 1.8.0 确实有效。 Ans 感谢@xander-vermaak 的批处理脚本。我的应用只需要--hidden-import="googleapiclient"
    【解决方案2】:

    实际上只是在 windows 上遇到了这个问题,而 macOS 还可以。我正在使用 fbsPyQt5 构建。

    问题

    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-*/
      • 可能在某处&lt;pythonInstallLocation&gt;/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-*/ 文件夹从&lt;pythonInstallLocation&gt;/lib/site-packages/ 复制到已构建应用的目录(或您编译的python 脚本试图访问google-api-python-client 的任何位置。
    3. 尝试启动应用程序

    【讨论】:

    • 解决方案 2 多年来一直是我的首选。可以确认它适用于我的 google_api_python_client 以及其他一些(尽管它很乏味)。
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

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

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

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

      例如

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

      【讨论】:

        【解决方案5】:

        我的案例是一个 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'),     
        
        .......],
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            【解决方案7】:

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

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

            【讨论】:

              【解决方案8】:

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

              【讨论】:

                【解决方案9】:

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

                【讨论】:

                  猜你喜欢
                  • 2019-11-12
                  • 2021-02-18
                  • 2021-12-01
                  • 1970-01-01
                  • 2021-05-05
                  • 1970-01-01
                  • 2020-10-13
                  • 2016-01-30
                  • 2018-12-25
                  相关资源
                  最近更新 更多