Writing Python scripts |
|
Release 9.2
Last modified November 17, 2006 |
![]() ![]() ![]() Print all topics in : "Getting started with writing geoprocessing scripts" |
Before creating a script, you need to take a closer look at the PythonWin application. The Interactive Window allows the execution of a single line of Python code, with the resulting messages printed to the window.
By definition, Python scripts are modules, which is Python's highest level of code organization. Modules serve several purposes; the most obvious is the ability to save code to a reusable form, a script. Scripts can avoid duplication of code by using programs contained in other modules. Using the import statement, a program can import any number of functions, variables, or both contained in another module. A module creates a natural grouping and naming structure, which eliminates ambiguity when using commonly named programs.
Each time you create a script, you will import one or more modules. All geoprocessing scripts use either the arcgisscripting, or win32com module, to provide access to geoprocessing functionality. Other modules that are required to work with math, files, and the operating system may also be imported, depending on the requirements of your script.
Scripts should be well commented. Each script should contain a heading section that describes what it does, who created it, and when it was created, along with comments throughout the script itself that explain what it is doing. Use the pound symbol (#) to indicate a comment, which is a text string that should not be interpreted by Python. All text following a pound symbol will be ignored by the interpreter.
Inevitably, errors occur when you write and execute scripts. Syntax errors may be caught by Python before the script is run by running a syntax check, but other problems caused by typing errors, invalid property or method names or invalid parameter values can only be caught during the execution of the script. Without a debugging environment, you are left with the option of inserting print statements at critical points of the script so you can trace its execution path and variable values. A debugging environment allows you to step through the program and interrogate variables, check object validity, and evaluate expressions.
There are several other debugging options when you run a script. In the previous exercise, you stepped through the script line by line, but you may want to run the script and only have it stop at defined points called breakpoints.
View an illustration
Learn more about creating and debugging scripts
The PythonWin Editor, Debugger, and Immediate Command windows are all used when writing scripts in Python. Python scripts may be written in other applications, as PythonWin is not required to write a Python script. Scripts are simply text files; any text editor, such as Notepad or VI, can be used to author a script. The standard Python installation provides a default Python editor called Integrated DeveLopment Environment (IDLE), that also provides search capabilities and a symbolic debugger. IDLE is a good application for writing Python scripts, but the advantage of using PythonWin is the integration of its debugger with the interactive window in a standard Windows-style application.
The information contained here is not a Python language reference. Certain Python syntax and behavior are explained with respect to examples and concepts used to demonstrate how to write a geoprocessing script. Python is a rich language supporting a number of operating systems and programming libraries. You should obtain a suitable Python reference to augment the information you find here. For Python beginners, Learning Python by Mark Lutz and David Asher and published by O’Reilly & Associates is a good introduction to the language and is not overwhelming in scope. There are many other books on Python and its particular uses, with new ones being released regularly, so explore what is available. The Python Web site has full documentation for Python, but it is concise and developer oriented. There is a large online Python community with many online resources that are accessible from the Python home page.
Python enforces certain coding standards, such as indentation being part of the syntax and variable name restrictions. Other languages may not have such standards, but it is a good idea to follow a general set of rules so your scripts and the scripts of others inside and outside your organization provide good readability, portability, and consistency. The following points are suggestions and can be used to create your own coding guidelines.
Variable names |
myIntegerVariable = 1 |
Script names |
|
Indentation |
|
Arguments |
|
Duplication |
|
Comments |
|