Need further help on my journey to get C# app + IronPython + Numpy Library working together


I put a question as an answer to this post : How to install numpy and scipy for Ironpython27? Old method doesn't work in order to centralize all thing related, but I get reminded it wasn't the good way to do it, and I needed to start a new "ask a question" for my purpose. So here we are.

I'll copy paste it, but as it is somewhat long I'll sum up first for convenience :

  • My end goal is to have an application in C# (well in Unity to be exact) that will allow end user to either mod it via python script and/or get from other those community mods (as a python script)
  • So I choose to use IronPython instead of recommended alternative because it doesn't require end user to set up python on their device (I want back end python script work to be transparent for user who don't want to mod themselves)
  • My issue here is to get the numpy python library usable from any given python script my C# app will call behind IronPython.
  • After some try an error here I am and stuck so I need further help if possible.

Here is the copy past of my original answer :

Well I'm still on the same issue. Even though I understand IronPython and numpy(|scipy) haven't be designed to work along and IronPython itself isn't the main API between C# and Python anymore,

I still prefer IronPython for not having to depends on enduser to have python installed on their machine like require. And also numpy is a very used python module/library so I'd prefer to let the enduser the possibility to use numpy on their python script (transparently without having them to install python, so it is why I prefer IronPython for now) and use their script on my C# application via IronPython.

So what i Did on my Visual Studio 2019 C# console Application (for testing IronPython for now) is to get ironpython from nuget package via nuget manager inside Visual Studio (downgraded to ironpython 2.7.5 as someone said it was the last version he was able to make ironpython work with numpy).

Then get file from a github (following those instruction :

Copied the inside C:/Program Files (x86)/IronPython 2.7/Lib

run the script like explained in the link I put before.

(didn't have issue, but if you got an issue with the url, see point 5. explanation of previous post to modify IndexedRepos on your .ironpkg config file)

Now in Visual Studio I have a python script to test my C#application with.

import time
import sys
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib')
import os
sys.path.append(r'C:\Program Files (x86)\IronPython 2.7\Lib\site-packages')

import numpy as np

print("Inside python : ")

text = "Python text"

acceleration = np.array([0,0]) #m/s²
vitesse = np.array([10,2]) #m/s
position = np.array([120,50])

Inputs = {"acceleration" : [0,-9], "vitesse" : [10,2], "position" : [120,50]}

def func():
    print("inside 'func()' : "+text)

def get_newPosition():
    global acceleration
    global vitesse
    global position
    vitesse = vitesse + acceleration
    position = position + vitesse
    return position

def get_newPosition2():
    global Inputs
    Inputs["vitesse"] += Inputs["acceleration"]
    Inputs["position"] += Inputs["vitesse"]
    return Inputs["position"]

def ToVector(dct):
    print("calling ToVector")
    return {k:np.array(v) for k,v in dct.items()}

def print_Inputs(dct):
    print("Inside python print_Inputs() : "+ str(dct))
    #print("And iteritems(): : "+ str(dct.items()))

Inputs = ToVector(Inputs)

#print("new position is : " + str(get_newPosition()))

print("before calculing new position from Inputs : " + str(Inputs))

print("new position v2 is : " + str(get_newPosition2()))

print("after calculing new position from Inputs : " + str(Inputs))

This script (alone, not my application yet) work inside visual studio ironpython 2.7 x86 environment, but not on the ironpython 2.7 x64 environment.

Then when trying my C# application :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace ConsoleApp1
    class Program

        static void Main(string[] args)

            Console.WriteLine("IN C#");            

            ScriptEngine pyEngine = Python.CreateEngine();
            var source = pyEngine.CreateScriptSourceFromFile(@".\..\..\..\Python_scripts\");
            CompiledCode compiltedCode = source.Compile();

            ScriptScope scope = pyEngine.CreateScope();

            source.Execute(scope); //GOT an error here !

            scope.SetVariable("Inputs", new Dictionary<string, dynamic>()
                {"acceleration", new[]{0,-9}},
                {"vitesse", new[]{15,20}},
                {"position", new[]{500,500}}

            dynamic myFunc = scope.GetVariable("func");


I got an error (sorry, it's in French) :

Exception levée : 'System.TypeInitializationException' dans Microsoft.Dynamic.dll Une exception non gérée du type 'System.TypeInitializationException' s'est produite >dans Microsoft.Dynamic.dll Une exception a été levée par l'initialiseur de type pour 'NumpyDotNet.NpyCoreApi'.

That is caused by the fact this dll isn't found :

Exception interne 1 : DllNotFoundException : Impossible de charger la DLL 'NpyAccessLib': Le module >spécifié est introuvable. (Exception de HRESULT : 0x8007007E)

And I am not able to add a reference to this NpyAccessLib.dll inside visual studio as it return me this error :

The reference could not be added. Please make sure that the file is accesible, and that it is a valid assembly or COM component.

Well here I'm stuck as I don't know how to tell my C# app to reference this needed dll !?

Thanks for any help !

EDIT Ok manage to get NpyAccessLib "loaded" simply to get manually the dll inside bin/debug folder. Also note that this "work" when building explicitly in x86, but not in x64 (as I get an error inside NumpyDotNet.NpyCoreApi fot the constructor (cctor)), exactly like when I try to run my script within IronPython 2.7.5 x64 environment inside visual studio).

So yeah I suspect the/some dlls are 32 bit only.

Ok, so I get it a further step when building and run in x86 and putting manually the dll inside the building folder.

But now I have another error !

no module named mtrand

And I don't understand as mtrand.dll was one of the dll correctly referenced inside visual studio. I try to put manually the mtrand.dll (and all the dll of scipy/numpy actually) inside the build folder but that doesn't change anything, I still get

no module named mtrand

... So here I stuck again ...

asked on Stack Overflow Feb 14, 2021 by TRex • edited Feb 15, 2021 by TRex

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0