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.
Now 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!"

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

Not very exciting or difficult but at least it worked!




January 14th, 2006 at 2:12 pm
[...] learning python one man’s journey into python… « Writing my first python script [...]
June 6th, 2007 at 1:44 pm
hi!,
the program worked & that what i needed
now can u tell me where can i find a good python tutorial?

selsine Says:June 6th, 2007 at 1:51 pm
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
February 2nd, 2008 at 1:55 pm
Do you still use gedit?
I guess I’m just curious about what you prefer.

selsine Says:February 21st, 2008 at 9:13 pm
Hi nilamo,
I actually use Geany now for most of my Python programming, with a dash of Gedit in there for good measure.