Speaktech.in

Create python script executable

Create python script executable

Add in the beginning of script for python 3

#!/usr/bin/python3

Add in the beginning for python 2

#!/usr/bin/python

A linux user bob is logged into the system and possesses sudo privileges. He writes the following python script named myscript.py:

#!/usr/bin/pythonimport os    print os.getenv("USER")print os.getenv("SUDO_USER")

He then makes the script executable with chmod +x myscript.py and then executes his script with sudo privileges with the command:

sudo ./myscript.py

The output of that program will be (using python 2.x.x):

    root    bob

If bob runs the program sans sudo privileges with

./myscript.py

he will get the following output:

    bob    None