[Tutor] Running a program

Alan Gauld alan.gauld at btinternet.com
Tue May 1 16:57:34 CEST 2007

"Jessica Brink" <jbrink at npsd.k12.wi.us> wrote 
> I know this seems elementary, but if I write a program and 
> save it as a .py file, how do I then run that program on the 
> command line 

Just type "python script.py" at the OS prompt

Or in *nix you can just type script.py if you havbe a shebang 
line at the top.

> or in the Python Shell (GUI)?  

You can't run it as such but you can import it.
If the script has no 

if __name__ == '__main__':

stanza then it will run as if you had executed it, if it has 
the stanza then you need to call the main funbction manually:

>>> import myscript
>>> myscript.main()


Alan Gauld
Author of the Learn to Program web site

More information about the Tutor mailing list