Has anyone been able to use VS Code (not VS Studio) to debug using attach to process?

1519
4
06-18-2022 12:17 AM
I_AM_ERROR
Occasional Contributor

I'm interested if anyone (or if it's possible to) to use VS Code to to debug python toolboxes using the Attach to process functionality mentioned here:

https://pro.arcgis.com/en/pro-app/2.8/arcpy/get-started/debugging-python-code.htm

VS Code has the functionality and I think I'm configuring it correctly but when I start a debug session an begin stepping through break points nothing seems to be connected.

Is this possible with Pro? Has anyone done this succesfully?

0 Kudos
4 Replies
BlakeTerhune
MVP Regular Contributor

It seems others are struggling with this as well.

Has anyone successfully attached the VSCode Debugg... - Esri Community

Although it's referring to ArcMap, you could try this.

How to Debug Python Toolboxes in 3 Easy Steps (esri.com)

I_AM_ERROR
Occasional Contributor

Thanks, read through those and tried ESRI's recommendations but no luck so far....

0 Kudos
Brian_Wilson
Occasional Contributor III

Pretty much what they describe is what I do.  The bottom of the .pyt file has code that will run in VS Code but not in the actual toolbox in ArcGIS Pro. 

# Unit test
if __name__ == "__main__":

    class Messenger(object):
        def addMessage(self, message):
            print(message)

    hello = Hello_Tool()
    hello.execute(None, Messenger())

 

What have you tried so far?

 

0 Kudos
Brian_Wilson
Occasional Contributor III

I spent some time yesterday playing around with the "Attach to process" debugging. Your Python script under test has to have the debugging component in it, which I did with two lines. When the python runs, it opens port 5678 and then you can find the process and attach to it in VS Code. Everything works then - single step, setting breakpoints, changing values of variables... 

But sadly when I added that code to a Python Toolbox and ran it from the COMMAND LINE it worked, but when starting it running inside ArcGIS Pro it caused a new copy of Pro to spawn instead of launching the tool. So close! But useless. 

"""
Run this from the command line, then connect to it from VSCode.
Set a breakpoint inside the loop and it should stop there.
Step through (F10) the loop a few times.
When you are done you could set loop to False in the debugger
to see it exit the loop and terminate.
"""
import time
import debugpy
debugpy.listen(5678)

tock = 1
loop = True # Set this to False in the debugger to end the program.
while loop :
    print("Tick", tock)
    tock += 1
    time.sleep(1)
print("We're done here.")
0 Kudos