Hi,

I would like to call some functions in the scipy.linalg package from C#,
however I think the ImportModule function from the ScriptRuntime object is
not working correctly for modules with dots in the name.

To setup the project, I followed the instructions at

https://www.enthought.com/repo/.iron/

and then I created a C# console app, added references to IronPython,
Microsoft.CSharp and Microsoft.Scripting.  When I run the code below, the
GetVariable method of the ScriptScope object that is supposed to be
associated with scipy.linalg appears to be resolving to the scipy namespace
instead.  I also tried the code below with version 2.7.3 of IronPython
downloaded from ironpython.codeplex.com, but the code failed with an
identical result.

Am I doing something wrong, or missing something obvious here?

Cheers,
Matt


using System;
using System.Collections.Generic;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace IronPythonFromCSharp
{
    internal class Program
    {
        private static void Main(string[] args)
        {
            var options = new Dictionary<string, object>();
            options["Frames"] = true;
            options["FullFrames"] = true;

            ScriptRuntime pyRuntime = Python.CreateRuntime(options);

            dynamic sys = pyRuntime.GetSysModule();

            sys.path.append(@"c:\Program Files (x86)\IronPython 2.7");
            sys.path.append(@"c:\Program Files (x86)\IronPython 2.7\DLLs");
            sys.path.append(@"c:\Program Files (x86)\IronPython 2.7\Lib");
            sys.path.append(@"c:\Program Files (x86)\IronPython
2.7\Lib\site-packages");

            dynamic clr = pyRuntime.GetClrModule();
            clr.AddReference("mtrand.dll");

            dynamic linalg = pyRuntime.ImportModule("scipy.linalg");
            dynamic builtin = pyRuntime.GetBuiltinModule();
            dynamic numpy = pyRuntime.ImportModule("numpy");

            //getting std function (which is in scipy, not scipy.linalg)
call will succeed
            object std = linalg.GetVariable("std");

            //can invoke std with a list
            dynamic l = builtin.list();
            l.append(1);
            l.append(2);
            l.append(3);
            object stdDev = linalg.std(l);
            Console.WriteLine(stdDev);     // writes out the type of stdDev

            //trying to get inv, which is in scipy.linalg, will throw an
error
            object inv = linalg.GetVariable("inv");
        }
    }
}
_______________________________________________
Ironpython-users mailing list
Ironpython-users@python.org
http://mail.python.org/mailman/listinfo/ironpython-users

Reply via email to