GhPython Debugging in Visual Studio

Hi,
I’m looking into debugging GhPython scripts in Visual Studio. This works, except problems in identifying the file that holds the executed script. The problems are expected as GhPython does not get a file name and then execute that file, but rather it gets a string and when executing this script the debugger has not way to get information on the file containing the script. Visual Studio reports a ‘Source not available, frame not in module’ error and debugging is not really possible.

I see two ways to fix this: manually specify the file (that possible in Eclipse, VS does not seem to offer this possibility) or fix GhPyhton.

The later seems to be easy at first glance, as the PythonScript class offers a ExecuteFile() metod. If I execute a script using this method the debugger can identify the file. But I don’t see a way to get access to the GhPython inputs and outputs? PythonScrip provides the necessary methods to setup the scope for the script properly, but ExecuteFile() does not run the script in that scope (this probably makes sense usually, but not here).

So, and ideas to resolve the issue?

See you
Flo

PS: A workaround is to write a small GhPython script that imports the script to be debugged but I find that not very elegant.

Hi @frist

over the years, I developed the Grasshopper part of GhPython, and I am happy to help.

Did you test this, or is this an assumption?

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi Giulio,

Did you test this, or is this an assumption?

I tested it, and it works, VS or ptvsd identifies the stack frame and file correctly if ExecuteFile is used. The only problem is, that a variable created by PythonScript.SetVariable() is not available in the executed script. Or at least I did not make to get this to work, it works fine, however, if I use the ExecuteScript method to run a script, but than VS can’t identify the file.

See you
Florian

Hi Florian,

probably this Tuesday*, a new GhPython version will ship with Rhino 7 WIP. It will include a new execution mode meant for debugging. The specs of the bugfix are on report RH-53391.

image

In place of code, you will now be able to run both absolute files and files specified as relative to the Grasshopper definition. These files are run with ExecuteFile, which may or may not run as fast as with compiled memory code, but might help with debugging.

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

*if not, probably the next one.

Hi Giulio,
that sounds great. That is exactly what I treid to add to GhPuthon, but I failed at giving the external script access to the Grasshopper input and output variables. I’ll wait and test the new version as soon as it’s available.

See you
Flo

RH-53391 is fixed in the latest WIP

Hi Dan,
I tested GhPython in WIP 7.0.19176.14015 (06/25/2019) and indeed the ‘Input is path’ option is present and I’m able to execute a script, but how do I get access to the input and output variables?

See you
Flo

Hi Florian @frist,

you should type, in the .py file, the same things you would type in the GhPython editor. Where is the confusion coming from?

Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi Giulio,
no unfortunately that is not working.

Here is my test:

test.py (34 Bytes) GhPyWipTest.gh (13.1 KB)

The embedded script works as expected, it can access the input ‘myStr’.
The script streamt from a file in the old way works as expected.
The script given by filename in the new way does is executed, but not have access to the GhPython inputs and outputs.

I’m I missing something?

See you
Flo

Hi Florian,
yes I see the problem. I’ll try to have it fixed by Tuesday, when the next WIP should go out.
Thanks,

Giulio


Giulio Piacentino
for Robert McNeel & Associates
giulio@mcneel.com

Hi Giulio

I’ll try to have it fixed by Tuesday

Cool. :slight_smile:

If you really find the time to work on the problem, could you briefly explain how you solved it? I’d be interested because right no I do not see a nice solution.

One quick fix of course is to load the file and use the ExecuteScript to execute the script, but that would not solve the debugging issue.

The only thing I can think of is to use ExecuteScript to execute something like this:

import __builtin__
__builtin__.myStr=myStr
       
import importlib
extSript=importlib.import_module("test.py", package=None)
reload(extSript)

This gives test.py access to myStr, but it seems like a dirty hack (and I’m not even sure it how this would look like for outputs).

See you
Flo

I am adding a new method to ScriptScope: ScriptScope.ExecuteScriptInScope().
You are right: with the previous method it would not work.

Hello again, @frist, RH-53391 is tackled more in the latest WIP.

1 Like

Hi Giulio,
I tested the latest WIP version and it work as expected now. This is a really nice feature as it allows to edit and debug GhPython scripts in an external IDE. I’m using Visual Studio 2019 but, VS Code, Eclipse or any other IDE would work just as fine.

See you
Florian

3 Likes

Hi @frist, @piac,

Any chance you guys could collaborate on a step by step guide to setting things up to edit and debug GhPython scripts in Visual Studio? Great if it could end up in the Developer docs, too. I’ve read through this thread and looked elsewhere: the info may all be there, but if it is, it’s scattered and hiding from me!

Regards
Jeremy

Hi Jeremy

Any chance you guys could collaborate on a step by step guide to setting things up to edit and debug GhPython scripts in Visual Studio?

Sure, I already had two requests buy other people, and I started working on a detailed step by step guide, but I did not find the time to finish it. But it’s still on my to do list.

See you
Flo

6 Likes

I also heard that @DavidLeon was going to write something for the Dev Docs. I think he is on vacation this week, though.

4 Likes