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!

selsine

del.icio.us del.icio.us

8 Responses to “Writing my first python script”

  1. learning python » Blog Archive » RSS reader Part One
    Says:

    [...] learning python one man’s journey into python… « Writing my first python script [...]

  2. swaroop
    Says:

    hi!,
    the program worked & that what i needed
    now can u tell me where can i find a good python tutorial?

  3. selsine

    selsine
    Says:

    Hi swaroop,

    Take a look at the rest of this site as I’ve written a bunch of tutorials, you can find the tutorial index here: http://www.learningpython.com/tutorial-index/

    You should also check out Guido’s tutorial: http://docs.python.org/tut/

    Or this great list of tutorials: http://www.awaretek.com/tutorials.html

  4. nilamo
    Says:

    Do you still use gedit?
    I guess I’m just curious about what you prefer.

  5. selsine

    selsine
    Says:

    Hi nilamo,

    I actually use Geany now for most of my Python programming, with a dash of Gedit in there for good measure.

  6. Evan
    Says:

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

  7. Gian
    Says:

    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.
    Gian

  8. how to spin articles
    Says:

    The most beneficial article spinners are quite probably to create the type of outcomes you
    wish. Consumers who do not fuloly understand online content
    know very tiny about report rewriters.

Leave a Reply

 

Popular Posts