Impossible to load a Python module in GhPython

Hi all,

I’m having troubles importing (full) Python 2.7 library modules in GhPython.
It has been suggested multiple times on this forum to either:

  • put the module files directly in the path used by GhPython

  • use the sys module within the GhPython component :

      import sys 
      my_path = r'path\to\mymodule.py' 
      if not my_path in sys.path: sys.path.append(my_path)
    

For some reason that the second option never worked for me (always leaving me on a Runtime error (ImportException): No module named 'nameofthemodule' error), so I decided to drop the module files in one of the directory used by GhPython instead (the IronPython folder in my case).

Unfortunately that second solution doesn’t seem to work either for me, GhPython being then unable to load correctly or entirely the library:

  • sometimes GhPython gives me an ImportException when trying to load a method of the library
  • other times the library loads but its functions/methods then fail to work

Examples with 3 python libraries, all installed with the pip command and then placed in the IronPython folder by hand:

–> with the noise, I get:

 Runtime error (ImportException): cannot import _perlin from noise

Traceback:
    line 12, in <module>, "C:\Program Files\IronPython 2.7\Lib\site-packages\noise\__init__.py"
    line 19, in script

–> with the noiselib library, I get:

Runtime error (ImportException): No module named _simplex

Traceback:
  line 6, in <module>, "C:\Program Files\IronPython 2.7\Lib\site-packages\noiselib\__init__.py"
  line 19, in script

–> with opensimplex, the library loads but when instantiating the OpenSimplex() class:

Runtime error (TypeErrorException): expected signed long, got long

Traceback:
  line 81, in overflow, "C:\Users\liukov\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\opensimplex\opensimplex.py"
  line 99, in __init__, "C:\Users\liukov\AppData\Roaming\McNeel\Rhinoceros\6.0\Plug-ins\IronPython (814d908a-e25c-493d-97e9-ee3861957f49)\settings\lib\opensimplex\opensimplex.py"
  line 21, in script

I should indicate that every library mentionned works perfectly fine outside Grasshopper (tested with the Windows prompt command or inside a Jupyter Notebook with Python 2.7)

questions::

  • Why do I keep getting these errors ? Is there something that I’m doing wrong ?
  • How can I make one of these libraries work with GhPython ?

PS: This is my very first post on this forum so please forgive me if this issue doesn’t belong to this category or if my explanation if lacking clarity. Thank you

Hello Liukov,

You say the modules work outside of GrassHopper : is that in Ironpython or in CPython 2.7? Noise contains c code so I would not expect it to work with Ironpython. I think your options will be to generate noise in a separate first step through CPython or to find a .Net compatible library for noise generation and import that through the clr.

Graham

1 Like

Hello,

Another option could be the GH CPython module however that, apparently, requires you to run as administrator.

I have not tried this…

Dear @Dancergraham,

Thank you for the swift reply and explanations.

It is with Python 2.7

You’re right but I believe opensimplex (3rd library linked above) is full python and, for this reason, should be working with GhPython. That signed long vs long error when instantiating the main class is still a mystery to me.

Following your suggestion I downloaded the LibNoise library and imported the assembly file through the clr module. Unfortunately I keep getting errors when instantiating classes.

For example:

import clr
clr.AddReference("LibNoise.dll")

import LibNoise
mod = LibNoise.IModule2D()

will return:

Runtime error (TypeErrorException): Cannot create instances of IModule2D because it is abstract

Traceback:
  line 17, in script

In parallel I’m trying to make GhPythonRemote work on my computer (ideally to have access to numpy and other scientific libraries) but, good lord, that thing is a nightmare to install.

I think you can use


but you need to change line 81
return c_long(x).value
to
return long(x)

2 Likes

Hi @chanley,

Thanks a ton for the workaround. It took me a while to understand you were talking about the OpenSimplex library not the Noise library but I got it working now. Thanks again !