2

My first question here. I am hobbyist self-learned python. I know this may be a silly question but I can't find any information after many days of searching.

I'm making an app to read data in google spreadsheet using Google API and then outputted PDF file with a nice picture of the pre-formatted layout using PYFPDF. It runs without any problem on my computer with Python3.5.2 + Pycharm + Mac OSX environment.

My app started by getting credential to access google API.

scope = ['https://spreadsheets.google.com/feeds']
creds = ServiceAccountCredentials.from_json_keyfile_name('my_app_creds.json', scope)
client = gspread.authorize(creds)

And my_app_creds.json file look like this:

{
  "type": "service_account",
  "project_id": "my_app",
  "private_key_id": "1234567890",
  "private_key": "-----BEGIN PRIVATE KEY-----\nVERY_LONG_PRIVATE_KEY\n-----END PRIVATE KEY-----\n",
  "client_email": "my_email@my_app.iam.gserviceaccount.com",
  "client_id": "0987654321",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/my_app.iam.gserviceaccount.com"
}

Things go wrong when I compiled package to make executable file in -F (onefolder) mode using pyinstaller. The app folder is successfully created and properly sit in 'dist' folder. But When I tried to execute the file, it throw in error:

FileNotFoundError: [Errno 2] No such file or directory: 'my_app_creds.json'
[24713] Failed to execute script my_app

Which I already include my_app_creds.json in to .spec file at a.datas, and I use the command as in pyinstaller documentation:

pyinstaller my_app.spec

my_app.spec is include .json file that 'Not found'

a = Analysis(['my_app.py'],
             pathex=['/Users/gungpetrucci/PycharmProjects/myapp'],
             binaries=[],
             datas=[('my_app_creds.json', '.')],

I verify my_app_creds.json is locate in the same folder as the executable file in 'dist/my_app'. I have no idea why the executable file can't find it?

As I tried to searching forums its seems unclear to me:

  • I don't sure that is it something to do with _MEIPASS? but I think _MEIPASS will be a concern only using -F onefile mode in pyinstaller?

  • Since the compiled app never run pass creds process, It never go to image processing process. I wonder that can the app locate .png picture that in a subfolder (before compile)? How can I include whole image folder into dist package and make it reachable?

Please help..

Faysal Ahmed
  • 7,501
  • 5
  • 28
  • 50
Harin Yu
  • 21
  • 3

1 Answers1

3

I faced same issue in one of my projects. I used following workaround -

  1. Crate a dictionary of contents from "my_app_creds.json":

    app_creds_dictionary = <paste contents of json file including {}>
    
  2. Use "from_json_keyfile_dict()" function for creating creds instead of file:

    creds = ServiceAccountCredentials.from_json_keyfile_dict(app_creds_dictionary, scope)
    
  3. Now as this is a part of your python file itself, no need to worry about pasting/maintaining copy of json file. Even .exe created in such a way also worked for me!

Hope this helps !

JustA.B.
  • 31
  • 4