Rudolph Musngi
Rudolph Musngi

Reputation: 165

Make python executable for both Windows and Mac (with dependencies)

I want to share my python script with someone who doesn't know what Python. That's why I want to make it to an executable (preferably for Mac and Windows but Windows is a priority).

I tried using pyinstaller and cx_freeze but they don't actually import the json file I need to run oauth2.

The traceback I get when I run the .exe is:

Traceback (most recent call last): File "MyScrip.py", line 9, in File "site-packages\oauth2client\service_account.py", line 219, in from_json_keyfile_name FileNotFoundError: [Errno 2] No such file or directory: 'creds.json' [11208] Failed to execute script MyScript

Anyone know how to do this? I tried Googling and looking for a solution but I am lost.

Thanks!

Upvotes: 1

Views: 799

Answers (1)

SyntaxVoid
SyntaxVoid

Reputation: 2623

Reposting the my comment-answer as an answer-answer.

The reason your exe was having a hard time finding the json file was because the use of relative paths instead of absolute paths. When the exe is compiled with pyinstaller, it's placed in a separate folder and any file-dependencies are not copied over.

The "quick" solution is to move the json into the same directory as the .exe but this can be troublesome if you compile frequently since you have to remember it everytime.

Instead of having to remember every time, you can write a .bat file that fully compiles your program with pyinstaller and moves all dependencies into the same directory as the .exe. The syntax is a bit different for every operating system.

Your script on Windows could look like this:

pyinstaller MyScript.py  # add any pyinstaller options you need
xcopy creds.json dist\main\creds.json

An automated "build" script is definitely beneficial when builds become more complicated.

Upvotes: 1

Related Questions