How do I use SciPy (e.g. scipy.optimize) from a C# Application?

Apr 25, 2012 at 2:43 AM

The SciPy for .NET docs ( suggest that a "custom C/C# interface" is available for accessing both SciPy and NumPy. I'd like to use functions from scipy.optimize inside a C# ASP.NET MVC Controller. I followed the referenced enthought installation instructions, and went looking for the correct DLL(s) to reference to use SciPy from within my project in C:\Program Files (x86)\IronPython 2.7\DLLs.

For NumPy: I found NumpyDotNet.dll, which contains a pretty straightforward-to-understand API (NpyArray, etc).

However I can't find an obvious DLL to import to access SciPy functionality. I tried importing optimize.dll, but it includes members like "__pyx_opt_args_5scipy_8optimize_5_nnls_fw_asfortranarray". Is this the .NET interface to SciPy.Optimize? How would I use this? Or is only NumPy wrapped for usage within C#?


Apr 25, 2012 at 3:01 AM

Hm, even with NumPy I'm left with some mystery how to use this. When I reference NumPyDotNet.dll and try to execute "var bar = NpyArray.Zeros()" I get an exception: Unable to load DLL 'NpyAccessLib': The specified module could not be found. But when I try to reference NpyAccessLib.dll I get an error about it not being a valid COM object.

Apr 25, 2012 at 3:44 PM

I don't think Numpy/Scipy can work entirely w/o IronPython, so you might consider using C# dynamic to use them from C# instead of using them directly.  You could use IronPython to import the module and then continue to interact w/ it dynamically from there.

Apr 25, 2012 at 5:39 PM

I'd assumed the custom C# interface loads and manages IronPython underneath the hood? The existence of NumpyDotNet, with its statically typed members, makes me think that at least NumpyDotNet is intended to be used directly from C#.

That said, I get the same Exception when I try to "import numpy" using IronPython+C# dynamic:

        private static void TestNumpy() {
            var source =
def SysPath():
    import sys
    return sys.path
def ImportNumpy():
    import numpy

            var engine = Python.CreateEngine();

            var sp = engine.GetSearchPaths();
            sp.Add(@"C:\Program Files (x86)\IronPython 2.7");
            sp.Add(@"C:\Program Files (x86)\IronPython 2.7\DLLs");
            sp.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib");
            sp.Add(@"C:\Program Files (x86)\IronPython 2.7\Lib\site-packages");

            var scope = engine.CreateScope();
            var ops = engine.Operations;

            engine.Execute(source, scope);

            dynamic py = scope;

            // Calling into IronPython works fine + sys.path looks correct
            dynamic syspath = py.SysPath();

            // EXCEPTION when we try to "import numpy":
            // "The type initializer for 'NumpyDotNet.NpyCoreApi' threw an exception.", 
            // "Unable to load DLL 'NpyAccessLib': The specified module could not be found. (Exception from HRESULT: 0x8007007E)"
            dynamic numpy = py.ImportNumpy();

Apr 25, 2012 at 5:49 PM

I'm not sure NpyAccessLib.dll as distributed is a valid DLL (?). When I try to Add Reference to it from w/in Visual Studio, I get:

"A reference to 'C:\Program Files (x86)\Iron Python 2.7\DLLs\NpyAccessLib.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM object"

Is this the correct forum to ask questions about IronPython/Scipy, or is there perhaps an enthought forum that is more appropriate?

Apr 25, 2012 at 5:58 PM

OTOH, I can "import numpy" from the console using ipy.exe. Is there something ipy does to initialize module paths, etc that I need to emulate in order to use numpy from within IronPython? I've never installed IronPython on this computer before yesterday, and I only installed the msi referenced from, so I assume ipy.exe is using the same IronPython as my Visual Studio code.

Apr 25, 2012 at 6:03 PM

This seems to be a 64-bit issue. If I use ipy64 and try "import numpy" I get the "Unable to load NpyAccessLib" exception. As a workaround, is there a way from C# to load a 32-bit IronPython instead of 64-bit?