<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Debugging a python script running in a toolbox in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318097#M68389</link>
    <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/10780"&gt;@Luke_Pinner&lt;/a&gt;&amp;nbsp;wrote:&lt;P class=""&gt;&lt;SPAN&gt;create a test/debug script that imports your script and calls the function&amp;nbsp;with hardcoded values and run the test/debug script in the IDE debugger.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Oh, that's an interesting idea. Could you share a sample of your calling script?&lt;/P&gt;</description>
    <pubDate>Fri, 11 Aug 2023 23:53:31 GMT</pubDate>
    <dc:creator>BlakeTerhune</dc:creator>
    <dc:date>2023-08-11T23:53:31Z</dc:date>
    <item>
      <title>Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318042#M68379</link>
      <description>&lt;P&gt;I'm investigating the capabilities of running a python script through a toolbox. I have a very simple script that appears to complete successfully, but as I get into writing more complicated logic, I'm wondering what techniques there are for debugging python toolbox scripts? I tried putting in a print() statement but I'm not able to figure out where I can view that output (if anywhere). If print statements can't be used, what are other techniques that can be used to debug scripts?&lt;BR /&gt;&lt;BR /&gt;Here's a screenshot of how I'm running a simple script and the code it contains:&lt;BR /&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Annotation-Annotation on 2023-08-11 at 13-39-11.png.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/77960i1A1A979740DEB07C/image-size/large?v=v2&amp;amp;px=999" role="button" title="Annotation-Annotation on 2023-08-11 at 13-39-11.png.png" alt="Annotation-Annotation on 2023-08-11 at 13-39-11.png.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 20:52:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318042#M68379</guid>
      <dc:creator>DonaldPike</dc:creator>
      <dc:date>2023-08-11T20:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318044#M68380</link>
      <description>&lt;P&gt;You need to use AddMessage.&lt;/P&gt;&lt;P&gt;I use this function to cover tool and standalone scripts... just pass it a text `msg`&lt;/P&gt;&lt;LI-CODE lang="python"&gt;def tweet(msg):
    """Print a message for both arcpy and python."""
    m = "\n{}\n".format(msg)
    AddMessage(m)
    print(m)&lt;/LI-CODE&gt;</description>
      <pubDate>Fri, 11 Aug 2023 21:06:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318044#M68380</guid>
      <dc:creator>DanPatterson</dc:creator>
      <dc:date>2023-08-11T21:06:43Z</dc:date>
    </item>
    <item>
      <title>Re: Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318046#M68381</link>
      <description>&lt;P&gt;The tools don't use the Python Window console, you aren't able to see print() output. Instead, use &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/functions/addmessage.htm" target="_blank" rel="noopener"&gt;arcpy.AddMessage()&lt;/A&gt;:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# bla bla
arcpy.AddMessage("This is a debug message, all is well...")
# bla bla&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The messages will be visible in the tool's details:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="JohannesLindner_0-1691788046087.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/77961i1D1E9E63256F9864/image-size/large?v=v2&amp;amp;px=999" role="button" title="JohannesLindner_0-1691788046087.png" alt="JohannesLindner_0-1691788046087.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 21:07:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318046#M68381</guid>
      <dc:creator>JohannesLindner</dc:creator>
      <dc:date>2023-08-11T21:07:45Z</dc:date>
    </item>
    <item>
      <title>Re: Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318048#M68382</link>
      <description>&lt;P&gt;For debugging your main code block, use the &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/writing-messages-in-a-python-toolbox.htm" target="_self"&gt;arcpy message functions&lt;/A&gt;&amp;nbsp;to write to the geoprocessing history output. For debugging code in updateMessages&lt;SPAN&gt;() when validating parameter inputs, I use the message &lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/customizing-tool-behavior-in-a-python-toolbox.htm#GUID-8BC6A4B7-B95D-4992-9792-482E1493FEED" target="_self"&gt;methods on the parameter object&lt;/A&gt;&amp;nbsp;to output debug info to the little tooltip messages that pop up on the tool interface in ArcGIS Pro. It looks like your toolbox doesn't have any parameters, so you might not need the parameter messaging.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Also keep in mind that when you make changes to the code of the toolbox, you need to restart ArcGIS Pro. There is a way around having to restart ArcGIS Pro, which you can find here:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/latest/arcpy/geoprocessing_and_python/editing-a-python-toolbox.htm" target="_blank"&gt;Editing a Python toolbox—ArcGIS Pro | Documentation&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 21:12:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318048#M68382</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-08-11T21:12:10Z</dc:date>
    </item>
    <item>
      <title>Re: Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318080#M68388</link>
      <description>&lt;P&gt;As you get into more complicated scripts, one way is to do all your testing and debugging in a proper IDE such as PyCharm or Visual Studio Code.&amp;nbsp; Keep all the logic in functions (just as you've got in your screenshot) and then either&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;in the &lt;FONT face="courier new,courier"&gt;if __name__ == "__main__":&lt;/FONT&gt; block&amp;nbsp; comment out the &lt;FONT face="courier new,courier"&gt;arcpy.GetParameterAsText&lt;/FONT&gt; lines and replace with hardcoded values and run in the IDE debugger, or&lt;/LI&gt;&lt;LI&gt;leave your script as-is, create a test/debug script that imports your script and calls the function&amp;nbsp;with hardcoded values and run the test/debug script in the IDE debugger.&amp;nbsp;&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Fri, 11 Aug 2023 22:42:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318080#M68388</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2023-08-11T22:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318097#M68389</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/10780"&gt;@Luke_Pinner&lt;/a&gt;&amp;nbsp;wrote:&lt;P class=""&gt;&lt;SPAN&gt;create a test/debug script that imports your script and calls the function&amp;nbsp;with hardcoded values and run the test/debug script in the IDE debugger.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Oh, that's an interesting idea. Could you share a sample of your calling script?&lt;/P&gt;</description>
      <pubDate>Fri, 11 Aug 2023 23:53:31 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318097#M68389</guid>
      <dc:creator>BlakeTerhune</dc:creator>
      <dc:date>2023-08-11T23:53:31Z</dc:date>
    </item>
    <item>
      <title>Re: Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318106#M68390</link>
      <description>&lt;P&gt;Here's one script structure you can use:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;from __future__ import annotations
import arcpy


def main(param1: int, param2: str):
    return str(param1) + " " + param2    


def execute(params: list[Parameter], messages = None):
    param1 = params[0].value
    param2 = params[1].valueAsText
    params[2].value = main(param1, param2)


if __name__ == "__main__":
    execute(arcpy.GetParameterInfo())&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This structure puts all the main program logic inside a "main" function that accepts standard Python types. If you run this through a standard toolbox (or bridge the call to execute from a Python toolbox) you extract the parameter values, pass them into "main" and set a derived parameter using the return value. However, you can import "main" from this file into a test file and call it with premade parameters and test the results. This makes it easier* to write unit tests for your script tools as well as launching the tool from outside of ArcGIS Pro which enables proper debugging. That said, you'll still need to AddMessage debug your parameter object related code if you don't have Visual Studio 2019 on hand.&lt;/P&gt;&lt;P&gt;&lt;FONT size="1 2 3 4 5 6 7"&gt;*The actual hard part with writing script tool tests is wrangling the GIS data and making meaningful comparisons against "good" data but that's a story for another day&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2023 00:11:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318106#M68390</guid>
      <dc:creator>DavidSolari</dc:creator>
      <dc:date>2023-08-12T00:11:05Z</dc:date>
    </item>
    <item>
      <title>Re: Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318109#M68391</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/191789"&gt;@BlakeTerhune&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;BLOCKQUOTE&gt;Oh, that's an interesting idea. Could you share a sample of your calling script?&lt;/BLOCKQUOTE&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Script tool (scripttool.py)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy

def main(parameter1, parameter2):
    #do something with parameters
    return

if __name__ == "__main__":
    #this won't get run when imported
    param1 = arcpy.GetParameterAsText(1)
    param2 = arcpy.GetParameterAsText(2)

    main(param1, param2)&lt;/LI-CODE&gt;&lt;P&gt;Test script (test.py)&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import scripttool

# Some hardcoded path for debugging
parameter1 = "D:/Temp/test2.shp" 
parameter2 = "D:/Temp/test2.shp" 

scripttool.main(parameter1, parameter2):&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 12 Aug 2023 00:19:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318109#M68391</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2023-08-12T00:19:21Z</dc:date>
    </item>
    <item>
      <title>Re: Debugging a python script running in a toolbox</title>
      <link>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318639#M68405</link>
      <description>&lt;P&gt;You can attach to the process via Visual Studio or PyCharm (both do validation and execution, VS also does debugging toolboxes).&amp;nbsp; esri provided a guide for it.&lt;/P&gt;&lt;P&gt;&lt;A href="https://pro.arcgis.com/en/pro-app/3.0/arcpy/get-started/debugging-python-code.htm" target="_blank" rel="noopener"&gt;get-started/debugging-python-code&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 15 Aug 2023 12:33:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/debugging-a-python-script-running-in-a-toolbox/m-p/1318639#M68405</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2023-08-15T12:33:16Z</dc:date>
    </item>
  </channel>
</rss>

