Show Navigation | Hide Navigation
You are here:
Geoprocessing > Automating your work with scripts > Getting started with writing geoprocessing scripts

Writing Python scripts

Release 9.2
Last modified November 17, 2006
E-mail This Topic Printable Version Give Us Feedback

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

Script editors

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.

Python references

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.

Coding guidelines

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
  • Variable names should start with a character and avoid using special characters such as an asterisk. If the variable name consists of several words, capitalize the first letter of each word (remember that Python and other languages are case sensitive), except for the first word. For example:
  • myIntegerVariable = 1

  • Use descriptive variable names and avoid using slang terms or abbreviations.
Script names
  • Script names should follow the variable naming guidelines above.
  • Python scripts should always have a .py extension.
  • Use indentation to show the structure of a program. Python enforces this, but other languages do not. Use two or four spaces to define each logical level. Align the beginning and end of statement blocks, and be consistent.
  • Use arguments to a script and avoid overuse of global variables, as they can cause unexpected results when they are accessed by a number of modules or scripts.
  • If a script has arguments, it should validate the input values and return a suitable message if an invalid value is specified.
  • Avoid duplication of code by placing commonly used functions in a common module that can be shared between scripts.
  • Scripts should be well commented. Each logical section should have an explanation.
  • Each script tool or function should have a header containing the script's name, a description of how it works, its requirements, who wrote it, and when it was written.

Please visit the Feedback page to comment or give suggestions on ArcGIS Desktop Help.
Copyright © Environmental Systems Research Institute, Inc.