PyInstaller file fails to execute script - DistributionNotFound
Asked Answered
W

7

9

I'm trying to convert my python file to an executable using PyInstaller. The program uses the Google Cloud Translate API to translate given text between languages. When running python quicktrans.py in the terminal, the program works fine. Then I ran pyinstaller quicktrans.py, SHIFT + right-clicked the directory the executable was in, and ran the .exe file in the terminal. This is the traceback that it spit out (Note this is not the whole traceback because it is a little lengthy):

File "c:\users\kalab\realpython\quicktrans\google\cloud\connection.py", line 31, in <module>
    get_distribution('google-cloud-core').version)
  File "site-packages\pkg_resources\__init__.py", line 559, in get_distribution
  File "site-packages\pkg_resources\__init__.py", line 433, in get_provider
  File "site-packages\pkg_resources\__init__.py", line 970, in require
  File "site-packages\pkg_resources\__init__.py", line 856, in resolve
pkg_resources.DistributionNotFound: The 'google-cloud-core' distribution was not found and is required by the application
Failed to execute script quicktrans

I've tried looking into this and some reason it's giving me a pip-like error. I've been trying to fix this for hours and no luck. Note: To install its client library, as per the documentation, you must run pip install --upgrade google-cloud-translate

I'm thinking this might have something to do with this because the last application I used dealt with the Facebook client module and you only had to do pip install facebook-sdk and the executable made by PyInstaller ran with no issues.

If you want to examine my code used in my program, it's hosted on my GitHub.

Thanks to anyone helping me out here!

Waxy answered 16/10, 2016 at 23:59 Comment(0)
C
1

My personal solution:

  1. Change all calls to get_distribution with it returned values (0.21.0 in my case)
  2. Remove from pkg_resources import get_distribution from import

for all files in the package.

Cogent answered 2/12, 2016 at 5:7 Comment(2)
Please try to give depth and clarity to your answers.Hemophilia
A humble request to look at the other answers and see for yourself.Hemophilia
M
7

It is basically package building name issue. Pyinstaller tries to import

google.cloud

where Google cloud package is now called

gcloud

. So you need to create a hook file for that names

C:\Users\\AppData\Local\Programs\Python\Python36-32\Lib\site-packages\PyInstaller\hooks\hook-gcloud.py

File contents:

from PyInstaller.utils.hooks import copy_metadata

datas = copy_metadata('gcloud')
Morette answered 19/10, 2018 at 19:45 Comment(1)
I don't know about you guys but I just did a pip install pip install google-cloud-core I think because there's an update already. It worked!Hebdomadal
J
3

Today, when I tried to build an EXE out of my Python script, I got the same error:

pkg_resources.DistributionNotFound: The 'google-cloud-core' distribution was not found and is required by the application

I thought the reason was one of the listed ones in this thread, since I was sure I had all dependencies installed with pipenv because my code compiled and I could debug and run the code without issues. Note I used pipenv shell in an empty folder and created my app in it, installing all necessary libraries using pipenv install ..., and one of the libraries was google-cloud-dialogflow (the app is a chatbot manager).

The solution was simply to run pipenv install google-cloud-core.

Now, pyinstaller chatbot_manager.py --onefile --windowed created the c:\Users\...\dist\chatbot_manager.exe file without issues.

Joanjoana answered 20/7, 2021 at 20:35 Comment(0)
D
2

In my experience base on the helps in https://github.com/GoogleCloudPlatform/google-cloud-python/issues/1187 :

  1. Go \Anaconda3\Lib\site-packages\PyInstaller\hooks folder (if you use anaconda otherwise you need to find it under python folder)
  2. Find the hook-google-cloud.py (If exist, otherwise you need to creat the hook.
  3. Write to existing code as shown below
    '''
    Copyright (c) 2017, PyInstaller Development Team.
    
    Distributed under the terms of the GNU General Public License with   exception
    for distributing bootloader.
    
    The full license is in the file COPYING.txt, distributed with this software.
    '''
    
    from PyInstaller.utils.hooks import copy_metadata
    
    datas = copy_metadata('google-cloud-core')
    
    datas += copy_metadata('google-cloud-translate')
    
    datas += copy_metadata('google-api-core')
    

Hope you find this explaination helpful. Thank you.

Donia answered 15/8, 2018 at 10:7 Comment(0)
M
2

Alternate hook tweak

I'm running into this same essential problem with the Google speech engine.

It's odd how everyone here seems to have success with slightly alternate solutions to this. I really don't understand how the "patches" to the hook which leave copy_metadata('google-cloud-core') in place can work? The error thrown back reads The 'google-cloud-core' distribution was not found..., so how can one execute that line as is?

This is my replacement for the file content of hook-google.cloud.py, in order to build an exe using google speech:

# PATCH: PROVIDED ALTERNATE PACKAGE NAME
from PyInstaller.utils.hooks import copy_metadata
try:
    datas = copy_metadata('google-cloud-core')
except:
    datas = copy_metadata('google-cloud-speech')
Murat answered 16/3, 2020 at 13:50 Comment(0)
C
1

My personal solution:

  1. Change all calls to get_distribution with it returned values (0.21.0 in my case)
  2. Remove from pkg_resources import get_distribution from import

for all files in the package.

Cogent answered 2/12, 2016 at 5:7 Comment(2)
Please try to give depth and clarity to your answers.Hemophilia
A humble request to look at the other answers and see for yourself.Hemophilia
S
1

I had the exact same issue. I solved it by doing this:

  1. Goto the Pyinstaller hooks folder (~\Lib\site-packages\PyInstaller\hooks)
  2. Find the file hook-google.cloud.py, open it, and add the following code to it
datas += copy_metadata('google-cloud-translate')
datas += copy_metadata('google-api-core')

The issue seems to be that get_distribution is not working with the default google.cloud.translate hook, so I just added this to a hook that was working.

Hope this helps someone.

Saraann answered 15/12, 2019 at 10:57 Comment(0)
C
0

I was using PyCharm venv for my project, and the only solution that worked for me is changing the project over to a system interpreter (and install the required packages to that).

Creighton answered 14/3, 2021 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.