6

I just want to make my script as an application. double-click and run instead of running in terminal. I have done it before with automator but now, on el capitan it doesn't work. It only gives error without explanation.

When I try with automator I get this error:

"The action “Run Shell Script” encountered an error."

Also I tried the trick below, still I am not able to do this.

#!/usr/bin/env python

chmod +x script.py

SOLVED:

After these two steps. I changed "Open with" to terminal for only this file and changed the #!/usr/bin/env python , it works. But it doesn't work without the two steps below, you need to follow all steps.

Add #!/usr/local/bin/python in the beginning of the code. Then run chmod +x myscript.py in terminal. After that change the application open with to Terminal.

It worked for me.

hzleonardo
  • 463
  • 1
  • 7
  • 16
  • Or, check [Easy way to launch Python scripts with the mouse in OS-X](http://stackoverflow.com/questions/14793391/easy-way-to-launch-python-scripts-with-the-mouse-in-os-x) – Hang Apr 12 '16 at 05:33
  • Save your script as `script.command`, then try it. – l'L'l Apr 12 '16 at 05:34
  • Hang I checked that page, didn't work and l'L'l I tried that one too. – hzleonardo Apr 12 '16 at 05:36
  • @hzleonardo: You should put the error you are getting in your question. – l'L'l Apr 12 '16 at 05:47
  • @l'L'l the error I get from automator is "The action “Run Shell Script” encountered an error." – hzleonardo Apr 12 '16 at 05:51
  • @hzleonardo: You should be using shell: `/bin/bash` not `/usr/bin/python` in automator for `Run Shell Script`. `chmod +x ...` is a shell command, not a python command. – l'L'l Apr 12 '16 at 06:04
  • @l'L'l Yes I know that. I added #!/usr/bin/env python in the beginning of the code and run chmod +x myscript.py in terminal. – hzleonardo Apr 12 '16 at 06:21
  • Q: Did you get this working? Ideally, the "out of the box" version of Python should do everything you want. Unfortunately, that doesn't seem to be the case for El Capitan. Next best, you should be able to install your own version. Unfortunately, you got "no software found" (because of another El Capitan changes). Q: Did installing with Homebrew work? Do you have everything you need again? Did this link help: http://docs.python-guide.org/en/latest/starting/install/osx/? – paulsm4 Apr 13 '16 at 22:58
  • @paulsm4 I tried to install with brew and dmg installation from the official python page, both didn't work. I guess I deleted pylauncher long time ago and the only way to get it back is setup/format os again. Os version changes do not get the pylauncher back too. So I looked for other solutions and found this one, now I can run it with double-click on py file without pylauncher. – hzleonardo Apr 15 '16 at 01:00

4 Answers4

6

I have changed the mode by

sudo chmod +x file-name.py

Then Added the following line on top of the file-name.py

#!/usr/bin/env python

Then run the file by running ./file-name.py command and it works fine.

Akhter-uz-zaman
  • 357
  • 3
  • 7
  • Note — For some reason, this does not work when launching via the systemwide Script menu. Workaround: directly specify the python command in the shebang, e.g. `#!/usr/bin/python`. – Mat Gessel Jan 29 '21 at 22:49
5

Quick step-by-step to create clickable .app to launch your python scripts.

Launch the Apple ScriptEditor (located in /Applications/Utilities/) and type in the following into the editor:

tell application "Terminal"
    do script with command "python /path/to/your/script.py"
end tell

After, simply hit save and choose to save as an application.

Ps.: If anyone reading this know how to get rid of the Terminal window that opens when you launch the .app, let me know.

If you want to get more advanced, check out Platypus.

3

Assuming Python is installed, this should work:

https://docs.python.org/2/using/mac.html

Select PythonLauncher as the default application to open your script (or any .py script) through the finder Info window and double-click it. PythonLauncher has various preferences to control how your script is launched. Option-dragging allows you to change these for one invocation, or use its Preferences menu to change things globally.

ADDENDUM:

http://docs.python-guide.org/en/latest/starting/install/osx/

The latest version of Mac OS X, El Capitan, comes with Python 2.7 out of the box.

You do not need to install or configure anything else to use Python. Having said that, I would strongly recommend that you install the tools and libraries described in the next section before you start building Python applications for real-world use. In particular, you should always install Setuptools, as it makes it much easier for you to use other third-party Python libraries.

The version of Python that ships with OS X is great for learning but it’s not good for development.

ADDENDUM 2:

Apple made some changes in El Capitan (including System Integrity Protection) that could cause installs to fail with the infamous "no software found to install". For example:

WORKAROUND:

Use Homebrew. Which is exactly what the Installing Python on Mac OS X I cited above recommends:

$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

$ vi ~/.profile => 
...  
export PATH=/usr/local/bin:/usr/local/sbin:$PATH

$ brew install python

Please let me know if this doesn't work for you.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • Thanks. I saw that page. I am using python 2.7 but pylauncher doesn't come with it and I couldn't find any download link for it. – hzleonardo Apr 12 '16 at 05:34
  • I'm not sure how you did your install, but download your .dmg from here: https://www.python.org/download/releases/2.7.4/ – paulsm4 Apr 12 '16 at 05:38
  • I installed long time ago and now I am a bit afraid to install again. Will it cause my packages lose if I reinstall python? – hzleonardo Apr 12 '16 at 05:39
  • thanks for the answer again but I have no problem with python. I just cannot find pythonlauncher. I tried to install it via terminal(easy_install,pip) but there is no packages. In Library/Python folder it doesn't exist too. So reinstalling is the only way it seems, but it gives error when I try to reinstall. "no software found to install" – hzleonardo Apr 12 '16 at 06:20
0

@https://stackoverflow.com/users/7885903/lucas-mendes-mota-da-fonseca

To hide the terminal window, I believe you could rename the .py to .pyw and call that.

https://stackoverflow.com/a/34739687/6713477