在C#中执行python脚本

Ala*_*Ala 5 c# ironpython python-2.7

我正在尝试在C#中执行python代码。通常,应该使用IronPython并在安装PTVS之后完成此操作(我使用的是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);
        }
Run Code Online (Sandbox Code Playgroud)

问题在于,IronPython似乎无法识别某些库,例如numpy,pylab或matplotlib。我看了一下,发现有些人在谈论Enthought Canopy或Anaconda,我都安装了它们而未解决问题。我该怎么做才能解决问题?

Ala*_*Ala 2

为了执行导入一些库(例如 numpy 和 pylab)的 Python 脚本,可以这样做:

        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); 
Run Code Online (Sandbox Code Playgroud)