Run Python Script on any Mac computer
Is there a way to create an executable Python script that will be able to run on any Mac via terminal, similar to a Bash script. I need any user to be able to just double click the file, and it should run. I want it to be able to run without the user having Python installed, but it has to be a Python script as it has to interact with an HTML button.
Solution 1:
MacOS comes with python 2.7 installed (and has done for nearly ten years). It also contains python 2.6 and 2.5 for compatibility with those versions, which were on even earlier OS releases.
So any python 2.7 -compatible script will run on MacOS. If you save the file with the file ending .command
or .tool
, it will launch in the Terminal from a double-click in the Finder. You will also need to set the execution flag with chmod
, as described in the other answer. (Some text editors, like BBEdit, will do this for you when you save the file.)
Make sure you've added #!/usr/bin/python
as the first line, and you're good to go.
Solution 2:
You can use PyInstaller, which is a free program that does the exact thing you want. As the official website says:
PyInstaller is a program that freezes (packages) Python programs into stand-alone executables, under Windows, Linux, Mac OS X, FreeBSD, Solaris and AIX. Its main advantages over similar tools are that PyInstaller works with Python 2.7 and 3.3—3.5, it builds smaller executables thanks to transparent compression, it is fully multi-platform, and use the OS support to load the dynamic libraries, thus ensuring full compatibility.
I should also mention that you can make a .py file executable just like other Linux(Unix) executable file, like this:
This command lets the file to be executed (see this)
user@linux~>chmod 0755 myProg.py
and then run it like this:
user@linux~>./myProg.py