# Numpy/Scipy for IronPython

**URL:** <https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98>\
**Category:** compas\_rhino\
**Created:** [November 5, 2018, 9:32am UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98 "2018-11-05T09:32:23Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 5, 2018, 9:32am UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/1 "2018-11-05T09:32:23Z")

</div>

I know compas.utilities/xfunc provides some numpy access for “fd” function, but is there a general way to use numpy/scipy functions in Rhino Ipython?

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 5, 2018, 4:39pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/2 "2018-11-05T16:39:07Z")

</div>

sure, you can use `XFunc` for anything. just make sure to set the serializer to `'pickle'` when using numerical data.

```python
import compas

from compas.utilities import XFunc

solve = XFunc('scipy.linalg.solve', serializer='pickle')

a = [[3, 2, 0], [1, -1, 0], [0, 5, 1]]
b = [2, 4, -1]

x = solve(a, b)

print(x)

```

should return `[2. -2. 9.]`

---

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 6, 2018, 3:19pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/3 "2018-11-06T15:19:00Z")

</div>

I get the following error at line x=solve(a,b), while running the code in Ipython for Rhino:

_Message: unexpected EOF while unpickling_

Also tried it for other functions with numpy:

**import compas**  
**from compas.utilities import XFunc**

**lis=[1,2,3]**  
**solve = XFunc(‘numpy.array’, serializer=‘pickle’)**  
**x=array(lis)**

**print(x)**

gives error at line x=array(lis): _Message: name ‘array’ is not defined_

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 6, 2018, 4:03pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/4 "2018-11-06T16:03:20Z")

</div>

just tried it in Rhino.  
i was wrong about the serializer.  
it should be set to `'json'`, which is default.

```python
import compas

from compas.utilities import XFunc

array = XFunc('numpy.array', serializer='json')
solve = XFunc('scipy.linalg.solve', serializer='json')

a = [[3, 2, 0], [1, -1, 0], [0, 5, 1]]
b = [2, 4, -1]

x = solve(a, b)
a = array(a)

print(x)
print(a)

```

see the attached image for the result…

 ![24](https://us1.discourse-cdn.com/flex015/uploads/compas_framework1/original/1X/f7a0f2cc72c1e1aae1d137d1e4ac22c1b0bd68e8.png)

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 6, 2018, 4:07pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/5 "2018-11-06T16:07:41Z")

</div>

by the way, it makes sense that your example doesn’t work, regardless of the type of serializer.  
for it to work, do something like

```python
import compas
from compas.utilities import XFunc

lis = [1, 2, 3]
array = XFunc('numpy.array')
x = array(lis)

print(x)

```

however, not sure what the point of that would be, because the array will be converted back to a list when the result is returned such that it can be used in Rhino…

---

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 6, 2018, 4:25pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/6 "2018-11-06T16:25:10Z")

</div>

“numpy.array” was just an example, but I still get the same error for numpy functions as before: _name ‘array’ is not defined_ or it could be any other function numpy.zeros: _name ‘zeros’ is not defined_

for the scipy one I get this error now:

Message: _No JSON object could be decoded_

_line 384, in raw\_decode, “C:\Program Files (x86)\IronPython 2.7\Lib\json\decoder.py”_

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 6, 2018, 4:41pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/7 "2018-11-06T16:41:16Z")

</div>

the “name ‘xxx’ is not defined” is because you have to change the name of your `XFunc` as i wrote in my previous answer. otherwise the name indeed does not exist.

```python
array = Xfunc('...')
result = array(...)

```

and not

```python
solve = Xfunc('...')
result = array(...)

```

as for the other error messages, it is a bit difficult to assess what is going wrong without more information. does `XFunc` work with `fd_numpy` of `dr_numpy`?

---

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 6, 2018, 4:49pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/8 "2018-11-06T16:49:07Z")

</div>

running the following code from [https://compas-dev.github.io/main/tutorial/xfuncs.html?highlight=numpy](https://compas-dev.github.io/main/tutorial/xfuncs.html?highlight=numpy) gives the same _No JSON object could be decoded_ error.

![Untitled](https://us1.discourse-cdn.com/flex015/uploads/compas_framework1/original/1X/649fcc10072b2be3c0618ead916c0e11b9730851.png)

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 6, 2018, 5:01pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/9 "2018-11-06T17:01:51Z")

</div>

do you have a CPython version on your system with Numpy/Scipy installed?

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 6, 2018, 5:04pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/10 "2018-11-06T17:04:26Z")

</div>

in other words, can you open a terminal running python and then import `compas`, `numpy`, and `scipy`?

```bash
$ python

```

```bash
>>> import compas
>>> import numpy
>>> import scipy

```

---

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 6, 2018, 5:17pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/11 "2018-11-06T17:17:08Z")

</div>

Well, for my normal coding with python I use _numpy_ and _scipy_ everyday. For example, the same code runs in eclipse (see the picture), however for a long time I wanted to use some of the geometrical properties of _rhinoscriptsyntax_ with some algebraic formulations in the same environment.

 ![Untitled](https://us1.discourse-cdn.com/flex015/uploads/compas_framework1/original/1X/24327cb421c97918e761b5f4494c52c9c0ac04f9.png)

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 6, 2018, 5:33pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/12 "2018-11-06T17:33:24Z")

</div>

but does it work in the terminal (command prompt)?

eclipse uses its own environment variables, so the fact that it works in eclipse is no guarantee that it would work outside of eclipse…

---

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 6, 2018, 5:50pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/13 "2018-11-06T17:50:00Z")

</div>

It does not recognize compas:

 ![Untitled](https://us1.discourse-cdn.com/flex015/uploads/compas_framework1/original/1X/c80ccbbba779f7afbe99e1959ed9d711eebb15eb.png)

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 6, 2018, 6:01pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/14 "2018-11-06T18:01:19Z")

</div>

that means `compas` is not installed on your system.

since you used an anaconda prompt in your screenshot and it still didn’t work, i am going to assume you still have an older version of COMPAS which you installed by downloading the source folder and adding it to you PYTHONPATH in eclipse and to the Python Search Path in Rhino.

however, in that case your system still does not know about it. the PYTHONPATH in eclipse is limited to eclipse and has no effect outside of it.

to make COMPAS available system-wide, just add PYTHONPATH as an environment variable and make sure to point it to the COMPAS source folder.

`Control Panel > System > Advanced System Settings > Environment Variables`

---

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 7, 2018, 8:19am UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/15 "2018-11-07T08:19:52Z")

</div>

I added python path from Anaconda/pkgs to Environments and compas src folder was already added in Environment Variables, still could not import compas in Anaconda prompt.

I had to install compas with _conda install COMPAS_, in order to be able to import it in anaconda prompt. Now, I also added the compas folder from Anaconda/pkgs to the Environment Variables and to the Rhino Ipython path, but still get the json error in Rhino Ipython. As you can see in the previously posted picture from Ipython, it looks like that there is an error with the IronPython in Program Files (x86).

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 7, 2018, 9:23am UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/16 "2018-11-07T09:23:44Z")

</div>

bit difficult to offer advice at this point. seems like a configuration issue. perhaps you should stop by the office at some point. will save us a lot of time…

---

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 7, 2018, 12:07pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/17 "2018-11-07T12:07:14Z")

</div>

Sure, can I come over today?

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 7, 2018, 12:22pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/18 "2018-11-07T12:22:03Z")

</div>

yes, i am around until 4.30…

---

<div class="post-metadata">

**Author:** ![SalMo](https://avatars.discourse-cdn.com/v4/letter/s/a5b964/32.png) [@SalMo](https://forum.compas-framework.org/u/SalMo)\
**Post date:** [November 8, 2018, 4:23pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/19 "2018-11-08T16:23:22Z")

</div>

I was able to solve the problem: there exist also Environment Variables for the current user (not admin). I cleaned that up as you did for the other one (removed extra paths and old compas paths, …). Now xFunc works with Ipython. 🙂

---

<div class="post-metadata">

**Author:** ![tomvanmele](https://sea1.discourse-cdn.com/flex015/user_avatar/forum.compas-framework.org/tomvanmele/32/13_2.png) [@tomvanmele](https://forum.compas-framework.org/u/tomvanmele)\
**Post date:** [November 8, 2018, 4:32pm UTC](https://forum.compas-framework.org/t/numpy-scipy-for-ironpython/98/20 "2018-11-08T16:32:14Z")

</div>

ha, happy to hear that it works!
