2

Say, I have a python script named hello.py, which I run on mac as:

$ python hello.py  

What do I need to do to run it as:

$ hello  
aliteralmind
  • 19,847
  • 17
  • 77
  • 108
JohnPaul
  • 75
  • 1
  • 5
  • 1
    You need to create alias for the above command in bash_aliases file. – Avinash Raj Sep 18 '15 at 16:28
  • possible duplicate of [how to run python script without typing 'python ...'](http://stackoverflow.com/questions/4993621/how-to-run-python-script-without-typing-python) – tripleee Sep 21 '15 at 05:03

2 Answers2

9

Add a "shebang" to the top of the file to tell it how to run your script.

#!/usr/bin/env python

Then you need to mark the script as "executable":

chmod +x hello.py

Then you can just run it as ./hello.py instead of python hello.py.

To run it as just hello, you can rename the file from hello.py to hello and then copy it into a folder in your $PATH.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1
  1. add the following line at the beginning of the script:

#!/usr/bin/env python

  1. rename hello.py to hello

  2. change the script to be executable: chmod 755 hello

acw1668
  • 40,144
  • 5
  • 22
  • 34