Making executable Scripts in Unix/Linux using Python

In unix/linux, you can make executable shell scripts using Python! You can write your python codes in any file, give it executable permission, Then simply run it by double clicking! 🙂

The trick is…

At the first line of your script, type #! followed by location of your Python interpreter

print 'Start coding herer'          # Python codes goes here

Saving the file with any name, give it executable permission – open a terminal/shell and type

sudo chmod a+x your-filename-with-full-path

You’re done 🙂

The Unix env Lookup Trick

On some Unix systems, you can avoid hardcoding the path to the Python interpreter
by writing the special first-line comment like this:

 #!/usr/bin/env python
 ...script goes here...

When coded this way, the env program locates the Python interpreter according to
your system search path settings (i.e., in most Unix shells, by looking in all the direc-
tories listed in the PATH environment variable). This scheme can be more portable, as
you don’t need to hardcode a Python install path in the first line of all your scripts.

Quoted from Oreilly's Learning Python



3 thoughts on “Making executable Scripts in Unix/Linux using Python

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s