3

I'm using Ironpython for the first time to Import a Python Script to C#. I get the error "No module named numpy", but I don't know why. I read that I have to add my path of my modules to my python script. This is my python script:

import numpy as np
import sys
sys.path.append(r"C:\Users\abc\CSharp\PythonScriptExecution1\packages\IronPython.2.7.9\lib")
sys.path.append(r"C:\Users\abc\PycharmProjects\untitled3\venv\Lib")
sum = np.sum([0.5, 1.5])
print(sum)

The second path is the path which is also used as project interpreter in Pycharm for the python.exe.

My C# code is this:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PythonScriptExecution2
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Scripting.Hosting.ScriptEngine pythonEngine =
                IronPython.Hosting.Python.CreateEngine();
            // We execute this script from Visual Studio 
            // so the program will be executed from bin\Debug or bin\Release
            Microsoft.Scripting.Hosting.ScriptSource pythonScript =
                pythonEngine.CreateScriptSourceFromFile("C:/Users/abc/PycharmProjects/untitled3/test.py");
            pythonScript.Execute();
        }
    }
}

Running the Python script in Pycharm works fine, but importing it to C# results in the error mentioned above. Can someone help me how to set the right paths?

edit: If it doesn't work, does anyone know any other way to run a python script with C#?

csi
  • 230
  • 1
  • 7
  • 22
  • You need to download numpy for ironpython. -> https://stackoverflow.com/a/34630229/2315752 Good luck, I could not make it work. – Nekeniehl Feb 25 '19 at 10:07
  • thanks, but I could not make it work either... – csi Feb 25 '19 at 13:05
  • Sorry to hear that, the solution we found was to call directly the script within a `Process` to python.exe, so at the end it was like calling the script from cmd. There are workarounds to pass arguments and receive output out of the process class. – Nekeniehl Feb 25 '19 at 13:31
  • And BTW, before you try, Pythonnet had for us the same result. – Nekeniehl Feb 25 '19 at 16:18

1 Answers1

1

Related to How to add modules to Iron Python?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PythonScriptExecution2
{
    class Program
    {
        static void Main(string[] args)
        {
            Microsoft.Scripting.Hosting.ScriptEngine eEngine =
                IronPython.Hosting.Python.CreateEngine();
            // We execute this script from Visual Studio 
            // so the program will be executed from bin\Debug or bin\Release
            Microsoft.Scripting.Hosting.ScriptSource pythonScript =

            ICollection<string> searchPaths = engine.GetSearchPaths();
            searchPaths.Add(@"C:\Users\abc\CSharp\PythonScriptExecution1\packages\IronPython.2.7.9\lib");
            searchPaths.Add(@"C:\Users\abc\PycharmProjects\untitled3\venv\Lib");
            engine.SetSearchPaths(searchPaths);

            engine.CreateScriptSourceFromFile("C:/Users/abc/PycharmProjects/untitled3/test.py");
            pythonScript.Execute();
        }
    }
}
Alan Turing
  • 2,482
  • 17
  • 20
  • 1
    it doesn't work... I adapted your code that it is running, but the error is the same – csi Feb 25 '19 at 12:00