Grasshopper

algorithmic modeling for Rhino

I loaded a custom module using ghPython node. Its a library that I am developing so I am still making a lot changes to it. I wanted to test it in GH but once loaded in there seems to be no way to refresh it. I added a new class and it will not see it since it looks like ghPython component somehow stored the first iteration of the module. 

Any way to refresh it? 

Thank you,

Views: 2516

Replies to This Discussion

Hi Konrad

(I've moved this discussion to the Vb, C# and Python coding subforum.)

  • The first time you "import", within one instance of Rhino, your module, it will be found and pre-compiled for execution.
  • The second time you import, a module is just fetched from a cache.
  • The reload() Python function updates a loaded module, by searching for the module again.

However, if you are seeing old behavior, it is likely that you have two copies of the same script, and the old one is loaded before the new one is found. Make sure that the module you want to use is loaded from the right location.

Locations to look for are to be seen in the _EditPythonScript editor Options dialog -> Module Search Paths; as well as the sys.path attribute of the sys module. By default, GhPython adds the location of the .gh file that is being run, if that is saved to disk.

I hope this helps; thanks,

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

Giulio,

So I first tried:

sys.path.append("module path")

import module

module = reload(module)

print(dir(module))

and it still doesn't show all of the latest classes.

then I checked options under _EditPythonScript and made sure that my path is added there. 

I verified that by 

print(sys.path)

My correct path is there and my module *.py is in that folder. I also went through all of the paths that were in the sys.path to see if maybe a copy of my module was somehow moved to one of these folders, but i didnt find it there. It only exists in that one place that i specified. 

I tried sys.path.remove() and then sys.path.append() to refresh it and still nothing.

Of course I copied the code from one ghPython component to another in a fresh file and still nothing. 

I don't know what else I can do at this point. Is there a way to clear cache? Maybe if i can wipe it clean then i can reload all of the modules anew. 

Thank you,

Konrad

Hi Konrad,

again, just because you added the search path, it does not mean that the path you added is the one that is going to be used, if any of the preceding paths in sys.path have a module.py file that matches your import module.

Also, please always check that the path you add to sys.path is not already there, or you will end with too many identical...

See this discussion for an example...

Does it help?

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

Guilio, 

I understand that. That's why I said that I checked all of the paths under sys.path to make sure that my module is not in one of them. It is not. The only place on my machine that my module exists right now is that location that I specified. I also moved that path to the top of the list under _EditPythonScript > Options to make sure it has top priority while searching for modules. 

If statement on path appending is definitely a good tip. I will incorporate that into my script.

However, none of this solves my issue. Is there a way to clear rhino/GH cache? 

Konrad

Well, if you re-read the text above, there is just no cache when you close Rhino and open it again. Everything is executed again. Does it work in that case? If you are asking about in-process refresh, then use the reload() function I mentioned earlier.

Maybe, though, with "custom module" you are referring to a .Net assembly (a .dll file)? If so, those cannot be unloaded and you just need to restart Rhino.

At this point, if it's neither of these two, I am pretty sure that there is something else going on here... Could you post your definition by any chance.

Thanks

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

EDIT: An additional option would be to come up with a new and fully unique name for your module, like module_1032348592135 and importing that. This way we could rule out for sure the double-loading case. Btw, we'll get to the bottom of this and we will find the source of this, given enough time :)

Let me get home and test it from home before I bother you any more. I am growing more convinced that this must be some network safety/firewall issue at work that screws everything up. I will test from home machine and get back to you later tonight. 

-Konrad

Don't worry about this. It would be great if you answered what case exactly this is, and later, if a solution is found, you could share it.

Thank you,

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

So it seems to be working just fine. Here's what I did:

  • I searched my hard drive for "mantisshrimp" (my module name)
  • I deleted every *.py file that was found (that includes temp folder and even one in the desired location. i kept a copy of it on github so no harm there)
  • I incorporated reload() and "if is in sys.path" suggestions that you made into my code
  • ran my code  - expected error happens stating that no mantisshrimp library can be found
  • created a new *.py file and placed in desired location
  • run my code - WORKS LIKE A CHARM

Thank you, It must have been one of the temp files that was screwing everything up. I made sure that my search included all drives in on my pc and it worked. Now reload() takes care of it so no more closing Rhino. 

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service