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 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 such as 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 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.
![Note Note](rsrc/note.png)
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
The PythonWin installation can be accessed from the Python for Windows extensions project. After opening the link, select the latest available build, then select the 32-bit or 64-bit Python 2.7 installer executable that matches your Python installation.
ArcGIS for Desktop and ArcGIS Engine products will use a 32-bit Python executable; ArcGIS for Server and ArcGIS for Desktop—Background Geoprocessing (64-bit) products will use a 64-bit Python executable.
To clarify which version of Python you have installed, consult the release notes or enter the following in your Python interpreter (or the Python window):
import sys
print(sys.version)
If this returns a string that includes 32 bit such as the following example: '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]', this is a 32-bit version of Python; otherwise, if this returns a string that includes 64 bit such as the following example: '2.7.2 (default, Jun 12 2011, 14:24:46) [MSC v.1500 64 bit (AMD64)]', this is a 64-bit version of Python.
Other common IDEs include the following:
- Python IDE installed with Python
- PyScripter, available on the web at http://code.google.com/p/pyscripter
- Commercial systems, such as Wing IDE (http://wingware.com)
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.
- Python Tutorial is part of Python's own documentation.
- Python for Non-Programmers provides tutorials for those with limited programming experience.
- Python Language Reference describes the syntax and semantics of Python.