Executing and debugging Python
These steps build on the script created in Creating a new Python script.
-
Click the Check button
on PythonWin's Standard toolbar to check for syntax and indentation errors in your script. If an error is found, the cursor is placed at that location. Correct the syntax and check it again until there are no errors.
-
Click the Run button
to open the Run Script dialog box.
- Type the parameters required by the script: an input workspace, a clip feature class, an output workspace, and a XY tolerance. You can supply your own data or use tutorial data. The table below shows tutorial data you can use. Since the paths to the data contain spaces, enclose the entire path in quotes when using it as a parameter.
- Choose Step-through in the debugger as the debugging option and click OK.
-
Click the Step Over button
. This executes the current line of code that imports all the specified modules and moves to the next line.
Step: Executes the current line and waits at the next.
Step Over: Executes the current line. If the line includes a Python module, function, or method call, the module, function, or method is executed.
Step Out: If within a module, function, or method, its code is executed and the debugger returns to the code following the module, function, or method.
- Click the Step Over button several times to reach the try statement.
- In the Stack window, click the _main_module node.
- Click the Locals (Dict) node to display all local variables and their values.
- Click the Step Over button several times to move the cursor to the while statement. The three lines you execute create the list of available feature classes and populate the variable fc with the first name.
- In the Watch window, click <New Item> and type "fc". Press ENTER to change the expression to the variable name. You now see the value of the variable.
- Click the Step Over button twice to set the output workspace and move to the if statement.
- Click the Step Over button to move into the if statement's code block where the Clip tool will be executed.
- Click the Step Over button twice to execute the tool and retrieve the next feature class name from the feature class names enumeration. Notice the value of the fc variable in the Watch window.
-
Click the Watch window and Stack window buttons
on the Debugging toolbar so they are closed.
-
Click the Interactive window button
. Add the following code and press ENTER:
-
Click the Close button
on the Debugging toolbar to stop the execution of the script.
Path | Parameter |
---|---|
"C:\ArcGIS\ArcTutor\GP Service Examples\DriveTimePolygons\ToolData\SanFrancisco.gdb\Transportation" | Input workspace |
"D:\ArcGIS\ArcTutor\GP Service Examples\DriveTimePolygons\ToolData\SanFrancisco.gdb\BaseMap\Parks" | Clip feature class |
"D:\ArcGIS\ArcTutor\GP Service Examples\DriveTimePolygons\Scratch\Scratch.gdb" | Output workspace |
5 | XY tolerance |
The XY tolerance is optional in the Clip tool, so it could be omitted if you want to use the default XY tolerance for each input feature class.
Two windows open: the Watch window, to show the value of expressions you define, and the Stack window, to display the value of variables in the system's call stack (the Watchh and Stack windows can be turned on or off by clicking their buttons on the Debugging toolbar). The cursor is placed on the first line of the script that is interpreted by Python, which is the import statement. Adjust the size of the Python application window as needed to accommodate the new windows.
In addition to the Step Over button, the Debugging toolbar also includes the Step button and the Step Out button
.
You will see all the variables you have set in the Interactive window and the multi_clip script, as well as the modules you have imported. Use the Stack window to check the scope of your variables and their values, as well as the contents of all available modules.
arcpy.GetMessages()
All the messages from the Clip tool are printed to the Interactive window. Any object or variable can be used in the Interactive window during a debug session, so it can be used for object interrogation and variable manipulation.
![Tip Tip](rsrc/tip.png)