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) 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. Whether you are writing a single script or many, a Python IDE will accelerate your development time and is strongly recommended.

Like the Python window, Python IDEs have an interactive window that allows the execution of a single line of Python code, with the resulting messages printed to the window. In addition, Python IDEs have many features to help you enter, edit, check syntax, and debug Python code. The standard Python installation provides a basic Python editor, Integrated Development Environment (IDLE), that also provides search capabilities and a symbolic debugger.

Inevitably, errors occur when you write and execute scripts. An IDE provides a debugging environment that allows you to step through code and interrogate variables, check object validity, and evaluate expressions.

NoteNote:

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.

Python IDEs

As there are many Python IDEs to choose from, picking the right one can very well depend on the platform you use (Windows or Linux), specific features, or the cost. At a minimum, a good IDE will have the following features:

Amongst many, a few common Python IDEs include the following:

Installing PythonWin

The PythonWin installation can be accessed from the Python for Windows extensions project. After opening the link, select the latest available build and 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 DesktopBackground 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.5 (default, May 15 2013, 22:43:36) [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: Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)], this is a 64-bit version of Python.

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

3/3/2014