c# - 在 C# 中执行 python 脚本

标签 c# python-2.7 ironpython

我正在尝试在 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);
        }

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

最佳答案

为了执行导入一些库(例如 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); 

关于c# - 在 C# 中执行 python 脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33669450/

相关文章:

c# - 我需要解析 250 个文件,总计 1gb 的数据并将其上传到 SQL 服务器。我能比这更有效率吗?

python - 反转数据框中的行值

python - IronPython WPF 加载新窗口

c# - C#、F#、IronPython 和 IronRuby 的集成

c# - C# 中的数组与泛型

c# - 将存档文件中的 Stream 转换为 Byte[]

java - 更简洁的正则表达式?

python - 执行谷歌搜索并返回结果数

Python OpenCV : Conversion from float to int for using cv2. 行()

python - 正确设置 Unity SerializedProperty 值