1

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.

  • 2
    But generally any Mac computer comes with OS X and has Python pre-installed. And isn't the requirement for CLI in Terminal a bit contradictory to "just double click"? Either is easily doable though. But you need to ask a more specific question. – techraf Jun 28 '16 at 6:17
3

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
2

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.

  • How do you double-click in Terminal? Also, I think that running chmod against the file is more important than adding a .command suffix here. – nohillside Feb 3 at 9:45
  • Double-click on a .command file in the Finder will run it in the Terminal. No exec flag needed. – benwiggy Feb 3 at 10:09
  • I just tried in Mojave, got a "The file foo.command can't get executed because you are missing the required access rights" error. It does work once I run chmod +x on it. – nohillside Feb 3 at 13:53
  • Ah. You're right. It certainly used to work in previous OS versions: I guess it's more tightening up of security. Interestingly, .tool which also associates the file to Terminal, doesn't produce the error, and it switches focus to Terminal, but doesn't run the script either. How annoying. – benwiggy Feb 3 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .