Sindre Sorhus
Sindre Sorhus

Reputation: 63487

Run python script without the "python" keyword

How can I run a python script in Terminal on Mac without using the "python" keyword, without having to edit my existing python files?

Right now I have to do this: python script.py

What I like to do is this: script.py

Upvotes: 12

Views: 11660

Answers (3)

manojlds
manojlds

Reputation: 301117

Add a shebang:

#!/usr/bin/python

or

#!/usr/bin/env python

I prefer the second one, since Python can be anywhere like /usr/bin/python, /usr/local/bin/python etc. and second one ensure that you don't have to keep editing the shebang.

And then you can just execute it as ./script.py if it is executable.

Upvotes: 19

ted
ted

Reputation: 2355

Try ./script.py instead of script.py ... or ensure your current directory is in your path and script.py should work....

Upvotes: 6

Troydm
Troydm

Reputation: 2670

in your python script add this as your first line

#!/usr/bin/python

then in terminal do this chmod +x ./yourpythonscript.py

then from terminal execute as ./yourpythonscript.py

Upvotes: 15

Related Questions