Writing my first python script

Well after reading the python documentation for a few days I decided it was time for me to write my first real script. Now I’ve written a few scripts before in the python interpreter, but I wanted to try my hand at creating some stand-alone executable file.

Now I didn’t want to go too crazy with my first script so I thought I would start off with the simplest program that everyone knows, the Hello World program. For this example I’m going to use the gedit since I already have it installed and it comes with built-in python syntax highlighting, but any standard or fancy text editor could be used in its place.

So to start gedit simply type ‘gedit’ in a terminal or find it in your gnome menu. Once gedit has started put it into pythong mode by selecting View | Highlight Mode | Scripts | Python from the menu.

geditNow I’m greeted with a blank gedit canvas and it’s time to write my first script. In order for a script to be executable on a Linux/Unix based OS it must start with the following line (or something similar):

#! /usr/bin/env python

Basically whatever follows the #! characters must locate the interpreter needed to execute the script file, in our case that’s the python interpreter. But instead of passing the path to the python interpreter (which we could have using something like #! /usr/bin/python) we use the env utility to search the PATH for the first executable named “python”. (PATH is an environment variable containing paths where executables can be found)

I found that there is a lot of debate about which form to use #! /usr/bin/env python or #! /usr/bin/python but for now I’m just going to stick with #! /usr/bin/env python since it appears to be the most portable.

After that I added a blank line to separate things and typed the following code:

print "Hello World!"

gedit helloworld.py listing

Not very complicated but it will get the job done. You’ll also notice, if you are working with gedit, the nice syntax highlighting. After that I saved my files as helloworld.py.

Now to execute this file we can open up a terminal, browse to the directory containing our helloworld.py file and type:

$ python helloworld.py

But that doesn’t make much sense since we went through all that trouble to type the #! line at the beginning of our file. So in order to make use of that we have to tell the system that helloworld.py is an executable file, to do that we need to modify its properties using chmod.

To do that we simply type:

$ chmod +x helloworld.py

Now the system knows that helloworld.py is an executable file and in order to run it we simple have to type the following in a terminal:

$ ./helloworld.py

(The ./ part of the above simple tells the terminal that we are looking in the current directory.)

helloworld.py results

Not very exciting or difficult but at least it worked!

8 thoughts on “Writing my first python script”

  1. terminal always tells me python: can’t open file ‘helloworld.py’: [Errno 2] No such file or directory
    ive tried everything
    any help?

  2. Selsine,

    Whenever I try to type $ python helloworld.py into my terminal it says “$” is not a recognised command. Any help would be greatly appreciated.

Leave a Reply

Your email address will not be published. Required fields are marked *