Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyInstaller file fails to execute script - DistributionNotFound

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!

like image 628
Mangohero1 Avatar asked Oct 16 '16 23:10

Mangohero1


2 Answers

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')
like image 152
Arun K Avatar answered Sep 22 '22 02:09

Arun K


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')
like image 37
BuvinJ Avatar answered Sep 22 '22 02:09

BuvinJ