Grasshopper Debugging Python in VSCode

I just got into Rhino and Grasshopper for some Python programming work. I created the following “environment” (what’s the proper term here?):

Which uses this Python 3 script:

import os
from random import randint

z = x + y
print(f'{x}+{y}={z}')
print("random", randint(1, 10000))
print("PID", os.getpid())

This allows me to inspect the process ID of the running Python process. (I also output some random value to make sure the script actually did run again.)

Now I’d like to debug the script from Visual Studio Code, for which I’ve created this launch configuration:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Attach using Process Id",
            "type": "debugpy",
            "request": "attach",
            "processId": 10896
        }
    ]
}

The PID is hard-coded. When I press F5 (“Start Debugging”), the Debugger awaits the connection. When I also press F5 (“Recompute”) in Rhino, the Debugger connects (yay)!

Unfortunately, my break points are ignored, and I cannot inspect my variables.

Has anybody an idea, what’s going wrong here? Maybe the Python interpreter within Rhino isn’t running in debug mode. (How could I change this behaviour?)

It feels like I’m pretty close, but I have no idea how to finally get there…

Today, I tried out debugpy. I’m documenting my efforts here.

I instrumented my script as follows:

import os
from random import randint

import debugpy
print(dir(debugpy))

z = x + y
print(f'{x}+{y}={z}')
print("random", randint(1, 10000))
print("PID", os.getpid())

In order to run it, I had to install the debugpy package to the provided Python installation:

cd C:\Users\Patrick Bucher\.rhinocode\py39-rh8
pythonw.exe -m pip install debugpy

On the positive side, the library can be loaded:

So I changed my launch.json debug configuration accordingly (not connecting through a process, but through the socket):

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Python Debugger: Attach using Process Id",
            "type": "debugpy",
            "request": "attach",
            "connect": {"host": "localhost", "port": 5678}
        }
    ]
}

And listen to the debugger from my script:

import os
from random import randint

import debugpy
debugpy.listen(5678)

z = x + y
print(f'{x}+{y}={z}')
print("random", randint(1, 10000))
print("PID", os.getpid())

When I execute the script, I get the following message:

grafik

It translates roughly to “file type not supported by Rhino” and refers to the debugpy package.

Looking into the code, I found the (undocumented) argument in_process_debug_adapter, to be used as follows:

import debugpy
debugpy.listen(5678, in_process_debug_adapter=True)
debugpy.wait_for_client()

Now Rhino doesn’t crash, but debugger won’t connect.

EDIT Not there yet, but I figured out that I need to install the version 1.6.7.post1 for Python 3.9. I also need to specify the Python runtime that’s being used:

import debugpy
import os
from random import randint

try:
    debugpy.log_to('./logs')
except:
    pass
python_path = r"C:\Users\Patrick Bucher\.rhinocode\py39-rh8\pythonw.exe"
debugpy.configure(python=python_path)
debugpy.listen(('0.0.0.0', 5678))
debugpy.wait_for_client()

z = x + y
print(f'{x}+{y}={z}')
print("random", randint(1, 10000))
print("PID", os.getpid())

It doesn’t work yet, but the error is gone.