wpf-使用IronPython调用python代码

2021/8/27更新:c#执行python的exe文件

这个的主要问题是慢。我的项目只用到matplotlib和biopython,所以通过创建虚拟环境,装包,然后用pyinstaller打包的方式构建可执行文件。简单测了一下,文件读取然后绘制4w+点就花了10s多。。实在不能忍。所以c#调用exe还是放弃吧。

DateTime beforeDT = System.DateTime.Now;
string pyexePath = @"C:\XXXXXX\dist\drawGraph.exe";
Process p = new Process();
p.StartInfo.FileName = pyexePath;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.CreateNoWindow = true;
// 我的一个参数
p.StartInfo.Arguments = @"C:\XXXX\XXX.txt";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
Console.WriteLine(output);
DateTime afterDT = System.DateTime.Now;
TimeSpan ts = afterDT.Subtract(beforeDT);
Console.WriteLine("Time: {0}s", ts.TotalSeconds); 
p.Close();

先说结论

IronPython是一个帮助c#引用python代码的插件/库。但是,由于我的python代码中用到matplotlib和biopython,而这两个包都需要numpy,可是IronPython完全不支持numpy,所以我最终放弃了这个做法。因此本文只贴最最最简单的例子。
在这里插入图片描述

环境

win10 + vs 2019 + netframework 4.7.2

实例

首先在工具-NuGet包管理器中搜索IronPython,安装。我装的版本是 2.7.11。

这里我第一次傻X了,因为IronPython 2.7.*就是python 2.7,IronPython 3.4才是python 3的。

然后在wpf项目里放一个mywindow.py文件,内容为:

# -*- coding: utf-8 -*-
import sys

def text():
	return str(sys.path)

调用的地方这样写:

using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
public partial class WavePainter : Window
{
    
    
    public WavePainter()
    {
    
    
        InitializeComponent();
        //Console.WriteLine(AppDomain.CurrentDomain.BaseDirectory);
        string basePath = AppDomain.CurrentDomain.BaseDirectory;
        string pyFilePath = basePath + @"..\..\py\mywindow.py";
        ScriptRuntime pyRuntime = Python.CreateRuntime();
        // 这里开始到dynamic py...行前都不是必须的。是指定lib path位置的
        var engine = pyRuntime.GetEngine("python");
        var pyScope = engine.CreateScope();
        var paths = engine.GetSearchPaths();
        paths.Add(basePath + @"..\..\packages\IronPython.2.7.11\lib");
        engine.SetSearchPaths(paths);
        // 执行py文件
        dynamic py = pyRuntime.UseFile(pyFilePath);
        string a = py.text();
        tb1.Text = a;
    }
}

可以看到sys.path是真的加入内容了。
在这里插入图片描述
当前,只做简单运算、不引用其他包的py代码是可以跑起来的,但是如果引用诸如os这种基础包都会报错,提示模块不存在。那么,如果实在要用,应该装对应的Python环境,然后把对应路径加到sys.path中来,那就是能跑的。

但是前面也说了,numpy用不了,而我的客户都没有python环境,我也不能强迫他们装,所以只能采取别的方式了,下次再更新解决方案。

猜你喜欢

转载自blog.csdn.net/pxy7896/article/details/119929434
今日推荐