Issues with importing custom Python module?


I have a custom Python package/module with the following file structure:

  • package contains imports for os, random, lib1, and lib2. lib1 contains a class, as does lib2.

In Grasshopper, I add the absolute path of the package directory to sys.path, since it’s currently not located inside the default Rhino scripts folder.

import sys

if "absolute/path/package" not in sys.path:

import package # works fine!

As already indicated above, I’ve outsourced the importing of modules - like os, random, etc. - that I use frequently in both and, to
However, when I call classes and their methods in GHPython from and, I get errors, like global name 'os' is not defined, although I import os in

I’ve also noticed that no Python cache files (.pyc) get created, when I import package in GHPython?

Any ideas what I’m missing here?


You still need to import os, random and similar modules in any of your submodules. Submodules can’t see the upper scope.

*.pyc files are not an IronPython thing.

1 Like

Got it! Thanks. Just out of curiosity, is that because the *.pyc cache files are not an IronPython thing?

No, completely unrelated.

1 Like

One major inconvenience is that the GHPython component only seems to reflect changes made to the module files, when Rhino is restarted. Simply recomputing the Grasshopper solution doesn’t work. This makes debugging a nightmare! Thanks again.

Python uses reload().

1 Like

Good to know. Thank you for pointing this out!

Yes you can reload modules but

from p1r4t3 import b0y

Doesn’t seem to work.

1 Like

Yes, you’re right! reload() doesn’t work, in my case at least, but removing the module/package from sys.modules did the trick! :slight_smile:

I simply call del sys.modules["package"] at the bottom/last line of the script. Works perfectly!

1 Like

Reload is a function in Py 2.7:

from p1r4t3 import b0y
1 Like