Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run a python 3.4 script as an executable?

Tags:

python

bash

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  
like image 856
JohnPaul Avatar asked May 06 '26 01:05

JohnPaul


1 Answers

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.

like image 185
Rocket Hazmat Avatar answered May 08 '26 13:05

Rocket Hazmat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!