13

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?

Monomeeth
  • 1,257
  • 2
  • 14
  • 24
shadown
  • 133
  • 1
  • 1
  • 4

3 Answers3

17

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
  • 1,257
  • 2
  • 14
  • 24
  • 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 Jan 09 '17 at 01:03
  • @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 Jan 09 '17 at 04:20
  • 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` – EthanAlvaree Sep 23 '19 at 17:57
9

Install pyinstaller: pip install pyinstaller

Create executable: pyinstaller --onefile yourscriptname.py

This worked for me on MacOS Mojave 10.14.2

Daniel Tkach
  • 101
  • 1
  • 2
  • 4
4

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
  • 329
  • 3
  • 10