3
votes

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#?

1
You need to download numpy for ironpython. -> stackoverflow.com/a/34630229/2315752 Good luck, I could not make it work. - Nekeniehl
thanks, but I could not make it work either... - csi
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
And BTW, before you try, Pythonnet had for us the same result. - Nekeniehl

1 Answers

1
votes

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();
        }
    }
}