Initialization of GHPython

Hi Giulio,
Is there something I should or could answer here on the Ladybug discourse?

Cheers, wim

@annasong, @saeranv, @Mostapha (tentative mentions)

I have no way to answer on that topic, as the membership to the forum seems not to be public, but I will answer publicly here.

I believe the simplest explanation would be that the module is already imported. If it is already imported, it will not be imported again.

Thanks,

Giulio


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

Thanks for responding @piac. Out of curiousity, what is the order of the import method in Rhino? For example, is this how modules are imported:

  1. Modules in standard library (math, os, sys) that come with IronPython
  2. Modules specified in sys.path
  3. Directories in IRONPYTHONPATH environment variable (?)
  4. Modules in the Rhino scripts folder: C:\Users%USER%\AppData\Roaming\McNeel\Rhinoceros\6.0\scripts

And when does this import happen? Right when Grasshopper opens or when the first GHPython is put on the canvas?

Thanks,

Saeran

  • Out of curiousity, what is the order of the import method in Rhino?

    The order is the one by which they are appearing in sys.path (this is a Python feature, not a Rhino feature). See here.

  • And when does this import happen?

    imports always happen when the import statement is evaluated (this is a Python feature, not a Rhino feature).

GhPython needs some imports to be evaluated correctly in order to run. These do not include private scripts in general, but compiled component namespaces, for example, are evaluated at Grasshopper startup (in order to fill icons and component in the tabs). These imports are then evaluated at startup. If you just place a script in the "scripts" folder, the import is not by itself evaluated.

@piac,

The order is the one by which they are appearing in sys.path (this is a Python feature, not a Rhino feature). See here 2.

To clarify: modules imported from the sys.path are preceded, and (I think) succeeded by other modules, not defined in the sys.path in Python. I.e the standard math, os sys libraries are imported before sys.path.

GhPython needs some imports to be evaluated correctly in order to run. These do not include private scripts in general, but compiled component namespaces, for example, are evaluated at Grasshopper startup (in order to fill icons and component in the tabs). These imports are then evaluated at startup. If you just place a script in the “scripts” folder, the import is not by itself evaluated.

This might be an unclear question but, where is the IronPython interpreter in Grasshopper? Is it started when Grasshopper is started, and attached to the Grasshopper canvas (i.e their is only one interpreter, that is referenced by different GHPython components)? If that is an accurate description, I can see your point that the only way we can import new modules, in your example of the script added in the “scripts” folder, is if we restart Grasshopper.

Saeran

Yes, it seems there is some point you want to have proved, but I am unaware of exactly what. Sorry.

C:\Program Files\Rhino 6\Plug-ins\IronPython\

It may or may not. It is started when some plug-in requests it. Grasshopper requests it via GhPython.

Yes. There is only one interpreter in all of Rhino. An interpreter is extremely costly, alone. There are several “scopes” and “modules/packages”, though, of course.

I did not write that statement. You can perfectly import new modules after Grasshopper has started. If you import a module for some reason, and that module has finished importing, however, even if you later have code that modifies sys.path, that code will come too late to change the game… That is my point.

I did not write that statement. You can perfectly import new modules after Grasshopper has started. If you import a module for some reason, and that module has finished importing, however, even if you later have code that modifies sys.path , that code will come too late to change the game… That is my point.

Ah I see, my mistake.

Well, @piac, I appreciate your patience in explaining everything here. I still find the whole1 ghpython import process a little confusing, but I think I learned a lot in this discussion.

Thanks for your help,

Saeran

1 Like