benefit of chmod-ing Python script on mac

0

I wrote a simple python script

print("hey there")

which I execute with this code in the Terminal app:

python3.5 script.py

I've read that you can preface Python scripts with a hashbang and path to python, and than chmod +x script.py to make it executable binary.

So my script now looks like this:

#! /usr/local/bin/python3.5
print("hey there")

I can still run the script with python3.5 command. I can also use ./script.py, but I am not sure of what the benefit is with this approach. What is the benefit of using hasbang and chmoding a python script?

sanjihan

Posted 2016-09-21T11:44:58.330

Reputation: 571

Answers

3

The benefits are:

  1. Less typing (ok, not much, but every little helps!)

  2. You don't have to know what's in the file to run it - if you come back to it in a few years, how would you have known what version of python it was? You may even choose to get rid of the suffix, so you wouldn't even know what language it was without reading it.

  3. Executables will be coloured by your terminal program, making them easier to spot.

There are probably more I haven't thought of, but that seems like enough to make it worthwhile!

A couple of minor notes: You haven't made it an executable binary, just executable. Also it's called a shebang, not a hashbang.

Ian Bamforth

Posted 2016-09-21T11:44:58.330

Reputation: 183