How do I make a python file executable on macOS Sierra?

6

5

I've been looking around the internet for days now, just to find a way to make a python file executable. I've tried the (chmod +x filename.py) by adding the #!/usr/bin/env python at the top, but it seems that nothing works. Plus I'm using brew to install modules, so when I hit pip install py2app as in the example, I get a message saying: You have python already installed by home-brew.

Can someone please explain how I can make a python file executable on macOS Sierra?

shadown

Posted 2017-01-08T05:56:10.280

Reputation: 63

How are you trying to execute, and what is the result? – Gordon Davisson – 2017-01-08T08:54:51.517

I'm trying to execute it by just double clicking the file and it should pop up on terminal ,like a terminal app – shadown – 2017-01-09T00:56:18.200

Then Monomeeth's answer is the right approach. – Gordon Davisson – 2017-01-09T04:12:36.227

Answers

6

Try the following and let me know how you go:

  1. Ensure the first line of your Python script is #!/usr/bin/env python
  2. Change the extension of the file to .command (i.e. If the file you want to make executable is called Test.py, change it to Test.command)
  3. In Terminal make the Python script file executable by running chmod +x Test.command (obviously the Test.command will be whatever your file is from Step 2 above).

By following the above steps, you should be able to double-click your Python script within macOS Sierra and it will open a terminal window and run the script.

Monomeeth

Posted 2017-01-08T05:56:10.280

Reputation: 1 047

Thank you ,sir ,I tried what you said above the exact same way you explained it ,but same thing ,I saved my file in the desktop and rename it to (Test.command) and i tried the chmod +x Test.command on terminal ,and nothing happened it didn't create the executable version of the file ,I even clicked on the file after changing the extension and it ran in terminal but that's what I got " /Users/sh/Desktop/Test.command: line 4: print: command not found logout

" knowing that it's not even an executable version – shadown – 2017-01-09T01:03:19.693

@shadown: There's no separate "executable version" of the file; the chmod command makes that file executable. The error you're getting looks like it's from bash, which suggests that the script is running, but under the wrong interpreter. The interpreter is controlled by the first line of the script (the "shebang" line), so make sure that's correct. – Gordon Davisson – 2017-01-09T04:20:22.330

If you are using Python3 installed via Homebrew, and these instructions are not working, you will need the first line of your Python script instead to be: #!/usr/local/bin/python3 – Mathemanic – 2019-09-23T17:57:01.680

2

Install pyinstaller: pip install pyinstaller

Create executable: pyinstaller --onefile yourscriptname.py

This worked for me on MacOS Mojave 10.14.2

Daniel Tkach

Posted 2017-01-08T05:56:10.280

Reputation: 31

1

Which python are you targeting?

Did you install it with brew? It uses a different path.

which python3 or which python

Choose the one you want

Copy that output

Paste it at the top of your python file

add a #! in front of that path so it looks something like

#!/usr/local/bin/python3

Make sure to change the file permissions

chmod +x filename

Put that file in a folder that is in your path

Not sure if your folder is in your path?

echo $path

How to add that folder to your path?

Find your path first

echo $HOME

If you are using bash or zsh you might have something like this

In ~/.bash_profile or ~/.bashrc or ~/.zshrc at the bottom of your file

export PYTHON_UTILS="$HOME/code/python/utils"

export PATH="$PYTHON_UTILS:$PATH"

Consider removing the .py from your file bc it is not needed in this case

Close and open your terminal, which is sourcing your file by its path

And now you should be able to treat your python file similar to a bash command

You don't need to use python3 filename.py to run the file, you can just use filename

From anywhere on your filesystem!

jasonleonhard

Posted 2017-01-08T05:56:10.280

Reputation: 117

0

Just save your prog with .py and execute idle will open by default, then go to run tab and select Run module or after click on .py file press F5.

Prakash Singh

Posted 2017-01-08T05:56:10.280

Reputation: 1