Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

execute a python script in C#

I am trying to execute a python code in C#. Normally it should be done using IronPython and after installing PTVS (I'm using VS 2010).

        var pyEngine = Python.CreateEngine();  
        var pyScope = pyEngine.CreateScope();   

        try
        {
           pyEngine.ExecuteFile("plot.py", pyScope);

        }
        catch (Exception ex)
        {
            Console.WriteLine("There is a problem in your Python code: " + ex.Message);
        }

The problem is that it seems that IronPython doesn't recognize some libraries like numpy, pylab or matplotlib. I took a look a little bit and found some people talking about Enthought Canopy or Anaconda, which i have both installed without fixing the problem. What should I do to get the problem solved?

like image 214
Ala Avatar asked Oct 30 '22 15:10

Ala


1 Answers

In order to execute a Python script which imports some libraries such as numpy and pylab, it is possible to make this:

        string arg = string.Format(@"C:\Users\ayed\Desktop\IronPythonExamples\RunExternalScript\plot.py"); // Path to the Python code
    Process p = new Process();
    p.StartInfo = new ProcessStartInfo(@"D:\WinPython\WinPython-64bit-2.7.5.3\python-2.7.5.amd64\python.exe", arg);
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true; // Hide the command line window
    p.StartInfo.RedirectStandardOutput = false;
    p.StartInfo.RedirectStandardError = false;
    Process processChild = Process.Start(p.StartInfo); 
like image 85
Ala Avatar answered Nov 02 '22 11:11

Ala