Executing and debugging Python

These steps build on the script created in Creating a new Python script.

View a completed version of this script.

  1. Click the Check button 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.
  2. Click the Run button Run button to open the Run Script dialog box.
  3. 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.
  4. Path


    "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


    XY tolerance

    Tutorial data you can use with the script

    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.

  5. Choose Step-through in the debugger as the debugging option and click OK.
  6. 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 Watch window and Stack View 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.

  7. Click the Step Over button Step Over button. This executes the current line of code that imports all the specified modules and moves to the next line.
  8. In addition to the Step Over button, the Debugging toolbar also includes the Step button Step button and the Step Out button Step Out button.

    • Step button Step: Executes the current line and waits at the next.
    • Step Over button 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 button 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.
  9. Click the Step Over button several times to reach the try statement.
  10. In the Stack window, click the _main_module node.
  11. Click the Locals (Dict) node to display all local variables and their values.
  12. 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.

  13. 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.
  14. 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.
  15. Click the Step Over button twice to set the output workspace and move to the if statement.
  16. Click the Step Over button to move into the if statement's code block where the Clip tool will be executed.
  17. 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.
  18. Click the Watch window and Stack window buttons Watch window and Stack View buttons on the Debugging toolbar so they are closed.
  19. Click the Interactive window button Intermediate button. Add the following code and press ENTER:
  20. 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.

  21. Click the Close button Close button on the Debugging toolbar to stop the execution of the script.
  22. TipTip:
    String variables have the letter u preceding the value to indicate a Unicode string. Unicode is a 16-bit character set standard designed to facilitate multiple languages.

Related Topics