Python Script Mechanics

Some hints to help you get going.

Creating the Script

A Python program is just a text file. You can use any text (programmer's) editor. There are several on the Linux machines, including XEmacs, Kate, xvim, and nedit. You can also use one of the free IDEs like idle, PyShell, or (under Microsoft Windows) Pythonwin. There are also several commercial IDEs.

I'll first show you how to use IDLE. You will need to open a shell to log into the server machine, then type idle.


> ssh elric
Password:
Last login: Tue Feb  3 10:03:40 2004 from 192.168.4.156
Copyright (c) 1980, 1983, 1986, 1988, 1990, 1991, 1993, 1994
        The Regents of the University of California.  All rights reserved.

FreeBSD 5.2-RELEASE (GENERIC) #0: Sun Jan 11 04:21:45 GMT 2004

> idle

Click on "File" then "New Window" to create a new text edit window. This lets you type in any text. Paste in one of the examples then choose "File" followed by "Save". It asks for a file name so use the one I put in the comment.

Now that it's saved, click on "Run" then "Run Module". This runs the program and puts the output in the "*Python Shell*" window.

If I made a typing mistake then usually the program won't run and Python will gave an error message telling me where the problem was. If the error is hard to diagnose I can also ask idle to help debug the code. First turn on the debugger with "Debug" -> "Debugger". Then when I run the program again I am in the debugger.

If you want to learn more about Idle, here's some documentation.

Text Editors

If you prefer using one of the text editors you can create a Python program like any other text file.

By convention, Python script files end with the extension .py.

Most programmer editors have a Python mode that will auto-format your Python programs and highlight keywords. Python mode is usually activated automatically for files which end in .py. Many programs will use Python mode if the word "python" appears in the first line or the line


# -*- mode: python -*-
is in the first few lines of the program. You may have to save and reopen the file for this to work.

Running the Script

Option 1

Run the python program from the command line, giving it the name of the script file to run.

> python now.py
Now is 02-02-2004 at 19:55
or, more precisely, 2004-02-02 19:55:43.046953
>

Option 2

Put the magic comment #!/usr/bin/env python as the very first line in the program.

Code:


#!/usr/bin/env python

# now.py
import datetime
now = datetime.datetime.now()
print "Now is", now.strftime("%d-%m-%Y"), "at", now.strftime("%H:%M")
print "or, more precisely, %s" % now
Make the script executable with chmod +x now.py

> chmod +x now.py
Then run the program as if it's any other Unix program

> now.py
Now is 02-02-2004 at 19:55
or, more precisely, 2004-02-02 19:55:43.046953
>

Errors

An IDE helps point out where erors occured and will even take you to the line that caused a problem. It can even walk you through the code step-by-step. You don't have these features when you use most standard text editors. You'll have to read the error messages to see where the problem occured, or use print statements, or use the simple debugger which comes with Python. For this week we're going to use Idle because it helps with debugging.

Here are some of the errors you might get in Python



>>> import date
Traceback (most recent call last):
  File "", line 1, in ?
ImportError: No module named date
>>> 
>>> 
>>> import datetime
>>> datetime.now
Traceback (most recent call last):
  File "", line 1, in ?
AttributeError: 'module' object has no attribute 'now'
>>> 
>>> 
>>> datetime.datetime.now

>>> 
>>> 1+
  File "", line 1
    1+
     ^
SyntaxError: invalid syntax
>>> 
>>> print "Now is, now.strftime("%d-%m-%y")
  File "", line 1
    print "Now is, now.strftime("%d-%m-%y")
                                    ^
SyntaxError: invalid syntax
>>> prin "Hello!"
  File "", line 1
    prin "Hello!"
                ^
SyntaxError: invalid syntax
>>> 

This page based on Perl Script Mechanics.


Andrew Dalke
Last modified: Tue Feb 3 11:39:59 SAST 2004