Using tool dialog boxes is the most common way to execute geoprocessing operations for those new to geoprocessing. When only a single tool must be executed at a time, these are a good way to run operations. Geoprocessing tool dialog boxes are easy to use and provide immediate feedback by placing warning or error icons and messages next to parameters not being used correctly. However, there are other, more efficient ways to execute geoprocessing tools or operations, such as Python scripting.
The Python window is a fully interactive Python interpreter (or interface) that allows geoprocessing tools and python functionality to be executed inside an ArcGIS Desktop application. This window is the best location to directly access Python scripting functionality in ArcGIS. Skills learned in the Python window can be directly applied when creating more complex stand-alone Python scripts or Python script tools.
The simplest way to use Python in ArcGIS is to enter Python commands into the Python window. The Python window prompts with three greater-than symbols (>>>), indicating the first line of the code block to execute. Simple Python syntax can be immediately entered and executed from this first line. Since the Python code that is entered can be immediately executed by pressing the ENTER key, the Python window can become a useful location to run and view experimental code. If unsure how a particular Python command works, open the Python window and experiment until the command runs without raising an error.
There are several key features that make the Python window a valuable resource for running and experimenting with Python commands and syntax:
- All Python functionality is exposed through the Python window.
- Multiline commands that contain more than one geoprocessing tool or geoprocessor method can be entered and executed.
- Tools or functions that have already been entered and executed can be recalled, edited, and reexecuted.
- Python commands or blocks of code can be loaded from existing Python files.
- Python commands or blocks of code can be saved to a Python or text file to reload later or used in a different environment.
- Autocompletion functionality makes filling in geoprocessing tool parameters quicker and easier than using tool dialog boxes.
>>> print "Hello Jack!"
Hello Jack!
>>> count = 0
>>> count
0
>>>
In the above example, a simple statement is printed and a variable is assigned a value. Notice after the print statement and after the variable count, the return value is echoed on the Python window.
Entering multiple commands
To enter lines after the first line without executing the code block, after entering the first line, hold down the CTRL key and press ENTER. The cursor moves to a secondary prompt (...) in the Python window, and an additional line of code can be entered. When finished entering all commands in this manner, press ENTER twice to execute the entire code block.
>>> x = 1
... y = 2
... print x + y
...
3
>>>
The Python window also presents a secondary prompt when more information is needed to complete a command. In the following example, the use of the logic for statement requires at least one more additional line to complete the code.
>>> for count in range(1,5):
... print count
...
1
2
3
4
>>>
Paths
Programming languages, such as Python, treat a backslash (\) as an escape character. For instance, \n represents a line feed, and \t represents a tab. When specifying a path, a forward slash (/) can be used in place of a backslash. Two backslashes can be used instead of one to avoid a syntax error. A string literal can also be used by placing the letter r before a string containing a backslash so it is interpreted correctly.
Example 1: Valid use of paths in Python
import arcpy
arcpy.GetCount_management("c:/temp/streams.shp")
arcpy.GetCount_management("c:\\temp\\streams.shp")
arcpy.GetCount_management(r"c:\temp\streams.shp")
Python window keyboard shortcuts
F1 | Shows the help for the current cursor location. |
F2 | Checks the syntax of the current line (or code block if in multiple line mode). Any errors will be shown in the Help pane. |
SHIFT or CTRL+ENTER | Enters multiple line mode. To exit multiple line mode (execute the code block), press the ENTER key on the last line. |
Up / Down | Access previously entered commands on the last line. |
Right-click | Access additional options. |