5

Let's say I've got this one-line Python module called say_hello.py.

print 'Hello World'

How can I make the script executable from any location in my terminal? That is, having Hello World printed outside the Python interpreter anywhere on my system. I'm running on OS X Mavericks.

  • http://stackoverflow.com/questions/2429511/why-do-people-write-usr-bin-env-python-on-the-first-line-of-a-python-script http://stackoverflow.com/questions/7366775/what-does-the-line-bin-sh-mean-in-a-unix-shell-script – Sam Jun 19 '14 at 05:57

2 Answers2

6

General *nix answer

First line of script should look something like:

#!/usr/bin/python

although the exact path may be different on your system. Then, make the script executable and put it somewhere in your PATH.

Ethan Furman
  • 52,296
  • 16
  • 127
  • 201
  • 3
    @trevorDashDash: save the file with no `.py` on it. You only need the `.py` if you want to be able to import the file. If you want both to import the script under certain situations and run it without the `.py` in others, you could create a link to the file. – Ethan Furman Jun 18 '14 at 21:46
0

Add as the first line of your script:

#!/usr/bin/env python

or, for a python3 script:

#!/usr/bin/env python3

The shell (actually the kernel) will use the first Python/Python3 interpreter found in your $PATH.

ndemarco
  • 168
  • 9