# Impossible to load a Python module in GhPython

**URL:** <https://discourse.mcneel.com/t/impossible-to-load-a-python-module-in-ghpython/84241>\
**Category:** Grasshopper\
**Tags:** ghpython\
**Created:** [June 3, 2019, 6:04pm UTC](https://discourse.mcneel.com/t/impossible-to-load-a-python-module-in-ghpython/84241 "2019-06-03T18:04:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![liukov.dobriev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/liukov.dobriev/32/683319_2.png) [@liukov.dobriev](https://discourse.mcneel.com/u/liukov.dobriev)\
**Post date:** [June 3, 2019, 6:04pm UTC](https://discourse.mcneel.com/t/impossible-to-load-a-python-module-in-ghpython/84241/1 "2019-06-03T18:04:04Z")

</div>

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 :

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](https://pypi.org/project/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](https://pypi.org/project/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](https://pypi.org/project/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

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [June 3, 2019, 7:06pm UTC](https://discourse.mcneel.com/t/impossible-to-load-a-python-module-in-ghpython/84241/2 "2019-06-03T19:06:50Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dancergraham](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/dancergraham/32/497397_2.png) [@Dancergraham](https://discourse.mcneel.com/u/Dancergraham)\
**Post date:** [June 4, 2019, 6:13am UTC](https://discourse.mcneel.com/t/impossible-to-load-a-python-module-in-ghpython/84241/3 "2019-06-04T06:13:24Z")

</div>

Hello,

Another option could be the [GH CPython module](https://www.food4rhino.com/app/ghcpython) however that, apparently, requires you to run as administrator.

I have not tried this…

---

<div class="post-metadata">

**Author:** ![liukov.dobriev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/liukov.dobriev/32/683319_2.png) [@liukov.dobriev](https://discourse.mcneel.com/u/liukov.dobriev)\
**Post date:** [June 4, 2019, 9:52am UTC](https://discourse.mcneel.com/t/impossible-to-load-a-python-module-in-ghpython/84241/4 "2019-06-04T09:52:37Z")

</div>

Dear @Dancergraham,

Thank you for the swift reply and explanations.

> [@Dancergraham](#):
>
> You say the modules work outside of GrassHopper : is that in Ironpython or in CPython 2.7?

It is with Python 2.7

> [@Dancergraham](#):
>
> Noise contains c code so I would not expect it to work with Ironpython.

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.

> [@Dancergraham](#):
>
> I think your options will be … or to find a .Net compatible library for noise generation

Following your suggestion I downloaded the [LibNoise](https://www.nuget.org/packages/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](https://www.food4rhino.com/app/gh-python-remote) work on my computer (ideally to have access to numpy and other scientific libraries) but, good lord, that thing is a nightmare to install.

---

<div class="post-metadata">

**Author:** ![chanley](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/chanley/32/341693_2.png) [@chanley](https://discourse.mcneel.com/u/chanley)\
**Post date:** [June 4, 2019, 12:11pm UTC](https://discourse.mcneel.com/t/impossible-to-load-a-python-module-in-ghpython/84241/5 "2019-06-04T12:11:08Z")

</div>

I think you can use

> **[noise](https://pypi.org/project/noise/)**
>
> Perlin noise for Python

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

---

<div class="post-metadata">

**Author:** ![liukov.dobriev](https://sea3.discourse-cdn.com/mcneel/user_avatar/discourse.mcneel.com/liukov.dobriev/32/683319_2.png) [@liukov.dobriev](https://discourse.mcneel.com/u/liukov.dobriev)\
**Post date:** [June 4, 2019, 10:55pm UTC](https://discourse.mcneel.com/t/impossible-to-load-a-python-module-in-ghpython/84241/6 "2019-06-04T22:55:15Z")

</div>

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 !
