Debugging GhPython components Visual Studio
This guide will walk you through to debugging python scripts in Grasshopper using Visual Studio.
- Rhino WIP
- Visual Studio with Python Tools for Visual Studio (PTVSD)
Visual Studio Setup
Open Visual Studio, and create a new Iron Python Project called GHPythonDebug.
Go to Debug > Options > Python > Debugging and tick Use Legacy Debugger
Inside GHPythonDebug.py type this and save the file:
import sys # This is the path where the Visual Studio Python modules are locate. # Change to your own: loc = r'C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\Extensions\Microsoft\python\Core' if loc not in sys.path: sys.path.append(loc) import ptvsd if not ptvsd.is_attached(): #set up secret, address and port for ptvsd ptvsd.enable_attach(secret = 'dev', address = ('localhost', 2019)) ptvsd.wait_for_attach() #in order for GH to wait for the process to be attached
Find the location of your VS ptvsd Python module:
For Visual Studio 2019 and Visual Studio 2017, the Python workload is installed in %ProgramFiles(x86)%\Microsoft Visual Studio<VS_version><VS_edition>Common7\IDE\Extensions\Microsoft\Python where **
** is 2019 or 2017 and ** ** is Community, Professional, or Enterprise.
(For earlier version, please head to the Microsoft site on where to find the path).
Make sure that you can access that folder. You should be able to find ptvsd folder in that location. After you’ve found the correct location, replace it to the
locvariable in the script.
Rhino WIP Setup
Start Rhino WIP (7), and type
_EditPythonScriptto open the editor. In Tools > Options > Script Engine, tick the Frames and Tracing checkboxes
Open Grasshopper. Create a new GHPython component, and choose Show Code and Input is path from the component options
Input the path of GHPythonDebug.py as a text into the
CodeInput of the component. Optionally, make the path pass through a
FilePathparam, right click it, and choose ‘Synchronise’. If test.py is saved in the same folder as the Grasshopper file, then you only need to input the file name. (At this point, Grasshopper will freeze momentarily waiting for PTVSD to attach)
Back in In Visual Studio, find in the top menu Debug > Attach to process…
In connection Type, choose Python Remote (ptvsd)
In the dialog just below the type (ptvsd) – it’s called “Connection target” in VS2019 – write :
tcp://dev@localhost:2019/and click Find. This corresponds to the secret, address and port that you defined in the code. (If clicking Find doesn’t work, type the address and just hit Enter).
Choose ‘Rhino’ in the Process view below, and “Attach”.
Now you should be able to run your script in GhPython. Breakpoints are hit, you can view and even modify locals (in the Debug->Windows-»Locals window in Visual Studio, available when debugging).