How to make python script executable on osx?

2019-06-26 04:01发布

问题:

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.

回答1:

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:

  • http://trac.wxwidgets.org/ticket/17203

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.



回答2:

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.



回答3:

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.