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.

1

2 Answers 2

7

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@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.
1

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.

1 Comment

just for reference: #!/usr/bin/python vs. #!/usr/bin/env python unix.stackexchange.com/q/29608

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.