Writing Python scripts

The Python window is where you enter code and execute it immediately. But you can also use an integrated development environment (IDE) such as PythonWin to create Python files on disk (files with a .py extension). These files, known as scripts, are programs you can execute from either the operating system prompt or by creating a script tool that executes the script. Scripts are simply text files; any text editor, such as Notepad or VI, can be used to author a script. But using a text editor to create a Python script is a poor choice compared to using an IDE like PythonWin.

Like the Python window, PythonWin has an interactive window that allows the execution of a single line of Python code, with the resulting messages printed to the window. In addition, PythonWin has many features to help you enter, edit, check syntax, and debug Python code. PythonWin is not required to write a Python script—it simply makes the task much easier. The standard Python installation provides a default Python editor, 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.


You can use the Python window to test ideas and work out small workflows. These ideas can then be saved out to a Python script, which can then be expanded and refined further using an IDE such as PythonWin.

Inevitably, errors occur when you write and execute scripts. In PythonWin (or any other Python IDE), syntax errors may be caught 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. A debugging environment allows you to step through the program and interrogate variables, check object validity, and evaluate expressions. (Without a debugging environment, you have the option of adding print statements at critical points of the script so you can trace its execution path and variable values.)

To learn more about using PythonWin, see Creating a new Python script.

Installing PythonWin

PythonWin is included with the installation media, but it is not installed by default. If you do not have the installation media readily available, the PythonWin installation can also be accessed from the Python for Windows extensions project.

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

A suitable Python reference book is strongly recommended to augment the information you find here. For Python beginners, Learning Python by Mark Lutz and David Ascher, published by O’Reilly & Associates, and Core Python Programming by Wesley J. Chun, published by Prentice Hall, are both good introductions to the language and are 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.

If you're new to Python, the external tutorials listed here are recommended for you.

Related Topics