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

Question:

I am currently trying to build an app with pyinstaller. I have gotten the error The 'google-api-python-client' distribution was not found and is required by the application and I’m completely lost why.

Running pip show google-api-python-client results with

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:devsoftwareschoology_scrapeschoology_scrape_venvlibsite-packages
Requires: google-auth-httplib2, uritemplate, google-auth, google-api-core, httplib2, six
Required-by:

I also have a requirements.txt file with all the libraries used in the project

Any help would be greatly appreciated!

Asked By: Hedgy

||

Answers:

Literally just ran into this issue on windows, whereas macOS is okay. I’m building with fbs and PyQt5.

The Problem

google-api-python-client is not a python module, but a resource, which means you cannot inject it as a hidden-import. googleapiclient.model reads the distribution info from google-api-python-client folder as a packaged resource.

Your full error might look closer to this:

...
File "c:python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packagesgoogleapiclienthttp.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:python36libsite-packagesPyInstallerloaderpyimod03_importers.py", line 627, in exec_module
    exec(bytecode, module.__dict__)
  File "site-packagesgoogleapiclientmodel.py", line 36, in <module>
  File "site-packagespkg_resources__init__.py", line 479, in get_distribution
  File "site-packagespkg_resources__init__.py", line 355, in get_provider
  File "site-packagespkg_resources__init__.py", line 898, in require
  File "site-packagespkg_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

Solution 1 – If using fbs or other common packaging framework

  1. Locate the google_api_python_client-*/
    • likely somewhere <pythonInstallLocation>/lib/site-packages/
  2. Copy google_api_python_client-*/ into your application’s src resource directory. For fbs this can be either:
    • src/freeze/windows/ (recommended), or
    • src/resources/windows/

Now when you fbs freeze and subsequently fbs installer your app, the google_api_python_client-*/ will be included in the built app’s directory alongside other googleapiclient python libraries, and the error should go away.

See: fbs project directory structure

Solution 2 – No auto-packaging hooks (untested):

If your packaging solution does not have similar hooks as above, then:

  1. Build your app
  2. Manually copy the google_api_python_client-*/ folder from <pythonInstallLocation>/lib/site-packages/ into the built app’s directory (or wherever your compiled python scripts are trying to access google-api-python-client.
  3. Try starting the app

Answered By: joeyipanimation

make sure that pip is linked to pip3 (Python 3) and not pip2 (Python2). On many OS(es) and distros, that’s still the case.

Check if that solves your problem:

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

If it did then add an alias to your .bashrc that links pip to pip3 and not pip2.

e.g.

echo "alias pip='pip3'" >> ~/.bashrc
Answered By: Charming Robot

If you are coding using PyCharm, do the next:

  1. Run $ pip3 freeze in the terminal
  2. Create the file "requirements.txt" in your project folder
  3. Copy the result of the first step into "requirements.txt"
  4. PyCharm will show the message that installed packages in /venv do not satisfy "requirements.txt". Click the "Install packages" button, wait until all the packages are installed and then build your app again.
Answered By: Farewell

My case is an Python-Flask windows app bundled with Pyinstaller –onefile option, using a .spec file.

I’ve copied the folder google_api_python_client-1.9.3.dist-info from the original location (maybe the windows site-packages folder) to the project folder.

Adding the following line to Pyinstaller spec file datas section (app.spec) was the solution for the problem.

a = Analysis(.......  

datas=[.....

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

.......],
Answered By: Cimei

I was able to find the solution here Link

Update your version of google-api-python-client to the version specified in the link (worked for me)

Also made a little .bat file:

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

Also of note: I ran the bat file in a virtual environment.

Answered By: xander vermaak

Copy Google directories from your Python Application install locations’ Python//Lib/site-packages directory to the dist/<dot_py_file> directory created by pyinstaller.

Answered By: K.Butler

I already posted a detailed answer in another question, but this question was the one where I fall down the rabbit hole, so it is only fair to give some feedback.

One liner that works for me:

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

SRC is in relative path form, you got to find the google_api_python_client-x.y.z.dist-info folder from your current working directory. So if you use venv, it means it should be something like:

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

And if you use let’s say conda for the environment, then you got to roll back until you find the same google_api_python_client… folder in that environment. For one build, the path was starting like this: /../../..

Answered By: Acibiber53

I got this error and was using the Serverless Framework. To fix, I simply removed the "slim: true" option in the "custom" block in serverless.yml

Answered By: humphriesj

As suggested by LMaiorano on GitHub,

service = build('drive', 'v3', credentials=credentials, static_discovery=False)

works for the latest version of google api client

Answered By: gokul gupta