1

I've been trying to run python in C# .Net environment. It succeed to compile and run python script without importing any libraries. But, I need to import numpy in that python script ran in C# .Net in order to Execute it properly. This is my source code, without importing libraries and succeed:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using IronPython.Hosting;
using Microsoft.CSharp.RuntimeBinder;

    namespace TestProject
    {
        class Program
        {
            static void Main(string[] args)
            {
                 Console.WriteLine("Enter the text you would like the script to print!");
                 var script =
                     "class MyClass:\r\n" +
                     "    def __init__(self):\r\n" +
                     "        pass\r\n" +
                     "    def go(self, input):\r\n" +
                     "        print('From dynamic python: ' + input)\r\n" +
                     "        return input";

                try
                {

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

                    engine.Execute(script, scope);
                    var pythonType = scope.GetVariable("MyClass");
                    dynamic instance = ops.CreateInstance(pythonType);
                    var value = instance.go(input);
                    Console.WriteLine(value);

                }
                catch (Exception ex)
                {
                    Console.WriteLine("Oops! There was an exception" +
                      " while running the script: " + ex.Message);
                }

                Console.WriteLine("Press enter to exit...");
                Console.ReadLine();

but, when I tried to import numpy :

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Diagnostics;
    using IronPython.Hosting;
    using Microsoft.CSharp.RuntimeBinder;

        namespace TestProject
        {
            class Program
            {
                static void Main(string[] args)
                {
                     Console.WriteLine("Enter the text you would like the script to print!");
                     var script =
                         "import numpy" //I added this module
                         "class MyClass:\r\n" +
                         "    def __init__(self):\r\n" +
                         "        pass\r\n" +
                         "    def go(self, input):\r\n" +
                         "        print('From dynamic python: ' + input)\r\n" +
                         "        return input";

                    try
                    {

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

                        engine.Execute(script, scope);
                        var pythonType = scope.GetVariable("MyClass");
                        dynamic instance = ops.CreateInstance(pythonType);
                        var value = instance.go(input);
                        Console.WriteLine(value);

                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("Oops! There was an exception" +
                          " while running the script: " + ex.Message);
                    }

                    Console.WriteLine("Press enter to exit...");
                    Console.ReadLine();

It gave me error :

No module named numpy

How to solve this, thank you.

mfathirirhas
  • 2,169
  • 5
  • 23
  • 35
  • There were projects to port `numpy` to IronPython (enthought), but I think they have been abandoned. See e.g. http://stackoverflow.com/questions/26852919/ironpython-vs-python-speed-with-numpy – roadrunner66 Mar 14 '16 at 08:07

1 Answers1

2

IronPython is a Python implementation on top of the .Net runtime. It is only able to import and use modules that are written in pure Python or are part of the standardlib distributed with IronPython itself. As far as I know, numpy is not one of those, it's a C-based extension.

I expect you installed some other C-based Python implementation (regular CPython, Anaconda or something else) then added numpy to it, and you are trying to call that from IronPython. That is just not possible.

The best you can do is to save your Python script in a .py file, then pass it as parameter to python.exe and retrieve the results. You can likely do that with regular .Net, look for ways of running any exe from C#.

Giacomo Lacava
  • 1,784
  • 13
  • 25
  • I don't know how to pass the value, I've seen such way to execute python file in .Net using 'Execute' or 'ExecuteFile', but still don't know how to return a value to .Net – mfathirirhas Mar 15 '16 at 09:47
  • Have a look at this: http://stackoverflow.com/questions/4291912/process-start-how-to-get-the-output – Giacomo Lacava Mar 15 '16 at 22:52
  • Giacomo Lacava , yeah that works, but the only problem is that I can't include non-pure ironpython libraries into c#.net, like numpy, and other. – mfathirirhas Mar 17 '16 at 15:26
  • Yes, my point was: you can develop your script with some other regular python interpreter (standard, ipython, anaconda, whatever) so you can use those libs, then write C# that will launch the script as a parameter to that interpreter exe, and retrieve simple output. That's really the only solution if you really have to use those libs and also C# for some reason. – Giacomo Lacava Mar 17 '16 at 15:30